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.
j, the nextgetis going to getj+2