0

You are given the following sequence of numbers, 1, 652 ,5, 15, 385, 4 , 55, 666, 13, 2, 4658, 9, 55, -588, 10, 1083, 17 ,4. You can remove the duplicate numbers by only using For loop and ArrayList.

public class Ex {
    public static void main(String[] args) {

        ArrayList list = new ArrayList();
        list.add(1);
        list.add(652);
        list.add(5);
        list.add(15);
        list.add(385);
        list.add(4);
        list.add(55);
        list.add(666);
        list.add(13);
        list.add(2);
        list.add(4658);
        list.add(9);
        list.add(55);
        list.add(-588);
        list.add(10);
        list.add(1083);
        list.add(17);
        list.add(4);


        System.out.println("Before remove : " + list);

        for (int i = 0; i < list.size(); i++) {
            for (int j = 1; j < list.size(); j++) {
                if (list.get(i) == list.get(j)) {
                    list.remove(j);
                }
            }
        }
        System.out.println("After remove duplicate items : "+list);
    }
}

Output

Before remove : [1, 652, 5, 15, 385, 4, 55, 666, 13, 2, 4658, 9, 55, -588, 10, 1083, 17, 4]
After remove duplicate items : [1, 5, 385, 55, 13, 4658, -588, 1083, 4]

Some items that not duplicated is missing. Ex 10 and 652.

5
  • 6
    And what's the question? Commented Oct 23, 2018 at 8:27
  • @NicholasK post updated. Commented Oct 23, 2018 at 8:30
  • what if i == j ? Commented Oct 23, 2018 at 8:31
  • After removing j, the next get is going to get j+2 Commented Oct 23, 2018 at 8:31
  • use list.sort and then iterate (once) again.If you have dublicates (and not triplecates) , you'll be fine. Commented Oct 23, 2018 at 8:32

6 Answers 6

4

You have many issues:

  1. You will remove non-duplicate records when i == j. To avoid that the inner loop should start with int j = i + 1.
  2. When you remove records, you should decrement j, since all the elements following the removed element are shifted one index down.
  3. You should compare Integers with equals, not with ==
Sign up to request clarification or add additional context in comments.

2 Comments

4. the raw type
@AndrewTobilko good point, but that doesn't affect the correctness of the code.
1

The inner loop always starts with j = 1 which means you will remove every single element of the list except the one at index 0. You should instead let j = i + 1. The inner loop will then only check the following elements of the list and not the one you are comparing. After removing you should use j-- so the index of the next element corresponds to j. And you can use == when comparing primitive types (int) When comparing reference types (Integer) you should use equals.

This is what i suggest:

for (int i = 0; i < list.size(); i++) {
        for (int j = i + 1; j < list.size(); j++) {
            if (list.get(i) == list.get(j)) {
                list.remove(j);
                j--;
            }
        }
    }

Comments

0

What about other values: 15 or 666? Are you sure you expect correct output? It is much better to use Iterator to remove elements from the list:

public static ArrayList<Integer> removeDuplicates(ArrayList<Integer> numbers) {
    ListIterator<Integer> it = numbers.listIterator();
    numbers.removeIf(num -> isDuplicated(numbers, it.previousIndex(), num));
    return numbers;
}

private static boolean isDuplicated(ArrayList<Integer> numbers, int toPos, int val) {
    for (int pos = 0; pos < toPos; pos++)
        if (numbers.get(pos) == val)
            return true;

    return false;
}

Output:

Before remove : [1, 652, 5, 15, 385, 4, 55, 666, 13, 2, 4658, 9, 55, -588, 10, 1083, 17, 4]
After remove duplicate items : [1, 652, 5, 15, 385, 4, 55, 666, 13, 2, 4658, 9, 55, -588, 10, 1083, 17, 4]

Comments

0
public class ArrayListExample {
    public static void main(String[] args) {
        // ArrayList with duplicate elements
        ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
        System.out.println(numbersList);
        Log.d("DBG",numbersList+"");
        // ArrayList without duplicate elements
        LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(numbersList); 
        ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet);
        System.out.println(listWithoutDuplicates);
        Log.d("DBG",listWithoutDuplicates+"");
    }
}

input

[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]

output

[1, 2, 3, 4, 5, 6, 7, 8]

Comments

0
// Code for Duplicates not allowed ... for upper example
for(int i = 0; i < list.size(); i++) {
            for(int j = i + 1; j < list.size(); j++) {
                if(list.get(i).equals(list.get(j))){
                    list.remove(j);
                    j--;
                }
            }

1 Comment

Please explain your answer
0

ublic class RemoveDublicate {

public static void main(String[] args) {
    List<Integer> list=new ArrayList<Integer>();
    list.add(1);
    list.add(652);
    list.add(5);
    list.add(15);
    list.add(385);
    list.add(4);
    list.add(55);
    list.add(666);
    list.add(13);
    list.add(2);
    list.add(4658);
    list.add(9);
    list.add(55);
    list.add(-588);
    list.add(10);
    list.add(1083);
    list.add(17);
    list.add(4);
        System.out.println(list);
        
        for(int i=0;i<list.size();i++)
        {
            for(int j=i+1;j<list.size();j++)
            {
                
                if(list.get(i)==list.get(j))
                {
                    list.remove(j);
                }
            }
        }
        System.out.println(list);


}

}

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.