Write a method called compressDuplicates that accepts a stack of integers as a parameter and that replaces

Question:

Write a method called compressDuplicates that accepts a stack of integers as a parameter and that replaces each sequence of duplicates with a pair of values: a count of the number of duplicates, followed by the actual duplicated number. For example, if the stack stores [2, 2, 2, 2, 2, −4, −4, −4, 82, 6, 6, 6, 6, 17, 17] , your method should change it to store [5, 2, 3, −4, 1, 82, 4, 6, 2, 17] . This new stack indicates that the original had 5 occurrences of 2 at the bottom of the stack followed by 3 occurrences of −4 followed by 1 occurrence of 82, and so on. If the stack is empty, your method should not change it. Use one queue as auxiliary storage.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: