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
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.
Step by Step Solution
3.44 Rating (163 Votes )
There are 3 Steps involved in it
public void compress Duplicates Stack s Queue q new LinkedList while sisEmpty q ... View full answer
Get step-by-step solutions from verified subject matter experts
