AI.
AI.

Reputation: 996

Remove duplicate items in ArrayList using for loop only

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.

Upvotes: 0

Views: 8122

Answers (6)

rushikesh sarolkar
rushikesh sarolkar

Reputation: 1

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);


}

}

Upvotes: 0

Ankit Tayal
Ankit Tayal

Reputation: 1

// 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--;
                }
            }

Upvotes: 0

Meysam Keshvari
Meysam Keshvari

Reputation: 1201

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]

Upvotes: 0

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

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]

Upvotes: 0

Bastmark
Bastmark

Reputation: 28

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--;
            }
        }
    }

Upvotes: 1

Eran
Eran

Reputation: 393791

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 ==

Upvotes: 4

Related Questions