I'm creating a program using LinkedList that could copy duplicate numbers to a new instance of linkedList.
Let's say List<Integer> orig = new LinkedList<>()
consists of these numbers: 23,1000,1000,1,23
I want to copy the duplicate numbers to List<Integer> copy = new LinkedList<>()
which supposed to be 23 and 1000 because I have 2 duplicate numbers. I tried testing it in an array and it works. But not sure why it won't work in LinkedList.
List<Integer> list1 = new LinkedList<>();
list1.add(23);
list1.add(1000);
list1.add(1000);
list1.add(1);
list1.add(23);
List<Integer> list2 = new LinkedList<>();
for(int x = 0; x <= list1.size()-1; x++) {
for(int y = x+1; y <= list1.size()-1; y++) {
if(list1.get(x)==list1.get(y)) {
list2.add(list1.get(x));
}
}
}
for(int y = 0; y <= list2.size()-1; y++) {
System.out.println(list2.get(y));
}
- Current OUTPUT:
23
- Expected output is:
23,1000