0

I'm working on a method which removes duplicates of an element in an ArrayList, recursively. But I'm running into a bit of a problem, my method works and removes some elements, but not all of the duplicates.

Here's my the input:

100, 200, 200, 300, 400, 300, 100, 500, 500, 400, 100, 400, 100, 100

And here's the output:

100, 200, 300, 400, 100, 500, 100

And my method:

public static void removeDuplicates(ArrayList<Integer> list, int counter){
    if(list == null){
        throw new NullPointerException();
    }

    if(counter < list.size()){
        if(list.contains(list.get(counter))){
            list.remove(list.lastIndexOf(list.get(counter)));
        }
        removeDuplicates(list, ++counter);
    }
}

I understand that I'm only removing the last element of said value, and then iterating to the next one. I was wondering how I should change this to remove all elements that are duplicates. Also, one part of my output that confuses me is, there are three values of '400', yet only one shows up in the output.

Thanks for the help.

4
  • 5
    It is worth noting that recursion is completely unnecessary here, and doesn't really buy you anything. Furthermore, your algorithm is very inefficient. Is there a reason you're doing things this way? Commented Mar 16, 2013 at 17:26
  • Why don't you use a HashSet? Commented Mar 16, 2013 at 17:26
  • If you remove a element from ArrayList, the size of the list will be one smaller. Commented Mar 16, 2013 at 17:28
  • It's for an assignment, we have to do this with recursion. I understand that a HashSet would work wonders, and that my code is completely inefficient but we're not worried about that. Commented Mar 16, 2013 at 17:36

5 Answers 5

2

Beside that @NPE is right (I assume this is homework), you should consider to call your function as long with the very same head, as long there is a duplicated element found. Use the next element (i.e., increase counter) only, if no duplicate is found (anymore).

Sign up to request clarification or add additional context in comments.

Comments

1

Try this one:

    public static void removeDuplicates(ArrayList<Integer> list, int counter){


        if(list == null){
            throw new NullPointerException();
        }

        if(counter < list.size()){
            if(list.contains(list.get(counter))){
                if(list.lastIndexOf(list.get(counter))!=counter)
                {
                    list.remove(list.lastIndexOf(list.get(counter)));
                    counter--;
                }
            }
            removeDuplicates(list, ++counter);
        }

    }

Comments

1

list.remove() will reduce list.size(), meaning everytime you remove an item and advance counter, you'll end up skipping one.

Comments

0

try

    if (counter < list.size()) {
        int i = list.lastIndexOf(list.get(counter));
        if (i > counter) {
            list.remove(i);
        } else {
            counter++;
        }
        removeDuplicates(list, counter);
    }

Comments

0

My first question is why are you using recursion? Much simpler to simply build a new list from the old one.

If you work through the sequence of calls, deleting the items from your string, you will find that the output is as expected.

Pass 1 removes the last 100

100, 200, 200, 300, 400, 300, 100, 500, 500, 400, 100, 400, 100

Pass 2 removes the last 200

100, 200, 300, 400, 300, 100, 500, 500, 400, 100, 400, 100

Pass 3 removes the last 300

100, 200, 300, 400, 100, 500, 500, 400, 100, 400, 100

Pass 4 removes the last 400

100, 200, 300, 400, 100, 500, 500, 400, 100, 100

Pass 5 removes the last 100

100, 200, 300, 400, 100, 500, 500, 400, 100

Pass 6 removes the last 500

100, 200, 300, 400, 100, 500, 400, 100

Pass 7 removes nothing Pass 8 removes nothing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.