Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Counting duplicates in a Stack

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Counting duplicates in a Stack

    Hey guys , I am having a problem with my code , consider this problem:


    Write a method 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, suppose a variable called s stores the following sequence of values:

    bottom [2, 2, 2, 2, 2, -5, -5, 3, 3, 3, 3, 4, 4, 1, 0, 17, 17] top

    If we make the call of compressDuplicates(s);, after the call s should store the following values:

    bottom [5, 2, 2, -5, 4, 3, 2, 4, 1, 1, 1, 0, 2, 17] top

    This new stack indicates that the original had 5 occurrences of 2 at the bottom of the stack followed by 2 occurrences of -5 followed by 4 occurrences of 3, and so on. This process works best when there are many duplicates in a row. For example, if the stack instead had stored:

    bottom [10, 20, 10, 20, 20, 10] top

    Then the resulting stack after the call ends up being longer than the original:

    bottom [1, 10, 1, 20, 1, 10, 2, 20, 1, 10] top

    If the stack is empty, your method should not change it. You may use one queue as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You may not use recursion to solve this problem. For full credit your code must run in O(n) time where n is the number of elements of the original stack.




    I wrote a code but still having a problem with it , and guys am I allowed to use 3 while loops ? Thanks !
     
    public void compressDuplicates(Stack<Integer> s ){
     Stack<Integer> backup= new Stack<Integer>();
        int count = 1;
        while(!s.isEmpty()){
         int temp = s.pop();
            backup.push(temp);
            while (!s.isEmpty()){
               int temp2 = s.pop();
                backup.push(temp2);
                    if( temp == temp2);
                        count++;
     
            }
                   backup.push(count);
        }
     
        while(!backup.isEmpty()){
        s.push(backup.pop());
        }
     
    }
     
     
     
    // example 1   Expected output : 	bottom [5, 2, 2, -5, 4, 3, 2, 4, 1, 1, 1, 0, 2, 17] top            My output:      	                //bottom [17, 2, 2, 2, 2, 2, -5, -5, 3, 3, 3, 3, 4, 4, 1, 0, 17, 17] top


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Counting duplicates in a Stack

    Do you have an algorithm for the program that you are trying to write code for?
    Can you post it? The code has no comments describing how it is trying to solve the problem. Without them, it is hard to say if the code is following the algorithm or not.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Counting duplicates in a Stack

    I don't think you need to make this problem so complicated. Break it down into a few simple rules.
    • Read element off stack and remember it
    • Keep popping until this number is different and count the number of times you pop
    • Once you encounter a different number, push your count then your number onto your temporary stack
    • When there are no more elements left in the original stack, reverse the order of the temp stack to get it into the correct orientation

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Counting duplicates in a Stack

    Taking Parranoia's suggestion a step further, Sets don't allow duplicate items, returning false when adding a duplicate item is attempted. Instead of "remembering it," items popped from the stack could be added to a Set. Any attempts to add that receive a 'false' response could be counted as duplicates. If identifying the duplicates is required, then the duplicate items could be stored in another collection, perhaps another Set if duplicate duplicates are not desired.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Counting duplicates in a Stack

    Quote Originally Posted by GregBrannon View Post
    Taking Parranoia's suggestion a step further, Sets don't allow duplicate items, returning false when adding a duplicate item is attempted. Instead of "remembering it," items popped from the stack could be added to a Set. Any attempts to add that receive a 'false' response could be counted as duplicates. If identifying the duplicates is required, then the duplicate items could be stored in another collection, perhaps another Set if duplicate duplicates are not desired.
    I'm not sure that would work quite right as it isn't gathering duplicates from throughout the stack, it is only gathering consecutive duplicates.

Similar Threads

  1. what is difference between call stack and stack tace?
    By me_shankara in forum Exceptions
    Replies: 6
    Last Post: October 27th, 2018, 03:23 AM
  2. Eliminating Duplicates in an Array
    By m2msucks in forum Collections and Generics
    Replies: 4
    Last Post: October 30th, 2011, 03:03 AM
  3. Duplicates in Arraylist
    By tcstcs in forum Collections and Generics
    Replies: 1
    Last Post: September 15th, 2011, 06:23 PM
  4. efficient way of checking duplicates
    By starmandell in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 2nd, 2011, 06:52 PM
  5. treemap Duplicates
    By debug in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2010, 11:52 AM

Tags for this Thread