0

(Full disclosure: this is for some homework I can't seem to figure out.)

The task: Identify duplicates in a list and add them to another ArrayList to be printed out.

Specifications: I am NOT allowed to use any collection other than an ArrayList, so I can't use something like a Set. It seems like every answer on StackOverflow recommends use of a Set, which is why I decided to ask this question.

What I've attempted so far:

public static void deleteDuplicates(List<String> list)
{
    int pointer = 1;
    List<String> duplicates = new ArrayList<String>();
    for (int i = 0; i < list.size() - 1; i++) {
        if (list.get(i).equals(list.get(pointer))) {
            duplicates.add(list.get(i));

            if (pointer == 1) {
                duplicates.add(list.get(pointer));
            } else if ((pointer + 1) == list.size() - 1) {
                duplicates.add(list.get(pointer));
            }

            pointer++;
        } else {
            display(duplicates);
            duplicates = new ArrayList<String>();
            pointer++;
        }
    }
}

The test data:

List<String> duplicated = new ArrayList<String>();
    duplicated.add("3");
    duplicated.add("3");
    duplicated.add("30");
    duplicated.add("46");
    duplicated.add("46");

What's not working: When the size of the list is an odd number, the duplicates report correctly. When the size of the list is an even number, only the first two duplicates are reported.

1
  • 1
    Do you want the duplicates to be removed from original list or just add the duplicates to another list. Commented Jul 24, 2014 at 13:22

3 Answers 3

2

The problem with your approach was the loop exits before it do the if-else check for last element. On the last iteration the if condition satisfies and it adds to duplicates but it wont enter the for loop again to goto the else part. So it does'nt get dispalyed. Try

public static void deleteDuplicates(List<String> list)
{
    int pointer = 1;
    List<String> duplicates = new ArrayList<String>();
    for (int i = 0; i < list.size() - 1; i++) {
        if (list.get(i).equals(list.get(pointer))) {
            duplicates.add(list.get(i));

            if (pointer == 1) {
                duplicates.add(list.get(pointer));
            } else if ((pointer + 1) == list.size() - 1) {
                duplicates.add(list.get(pointer));
            }

            pointer++;
        } else if(duplicates.size() > 0) {
            display(duplicates);
            duplicates.clear();
            pointer++;
        }
    }
    if(duplicates.size() > 0){
        display(duplicates);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Ali the question says sorted array.
yes my mistake it was on the title so I didnt see it. +1 for your answer:)
0

Although Syam S answer is right but this will work for unsorted array too:

public static void deleteDuplicates(List<String> list)
{
    List<String> duplicates = new ArrayList<String>();
    for (int j = 0; j < list.size() - 2; j++) {
        int pointer = j;
        for (int i = j+1; i < list.size() - 1; i++) {
            if (list.get(i).equals(list.get(j))) {
                duplicates.add(list.get(i));
                duplicates.add(list.get(j));
            } 
            if(duplicates.size() > 0){
                System.out.println(duplicates);
                duplicates.clear();
            }
        }
    }
}

you can see the working version in Ideone

Comments

0

Try this:

Extending the ArrayList

1)

boolean result  =   false;
            if(!contains(object))
                result= super.add(object);
            return result;

OR

2)

ArrayList<String> myList    =   new ArrayList<String>()
    {
        @Override
        public boolean add(String object)
        {
            boolean present =   false;
            boolean result  =   false;
            for(int i=0;i<size();i++)
            {
                if(object.equals(get(i)))
                {
                    present =   true;
                    break;
                }
            }
            if(!present)
                result= super.add(object);
            return result;
        }


    };


    myList.add("1");
    myList.add("2");
    myList.add("3");
    myList.add("1");
    myList.add("2");
    myList.add("3");
    myList.add("1");
    myList.add("1");
    System.out.println(myList);

1 Comment

In this case instead of a for loop you could use contains method of arraylist itself. if(!contains(object)) result = super.add(object);

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.