For example my list contains {4, 6, 6, 7, 7, 8} and I want final result = {6, 6, 7, 7}
One way is to loop through the list and eliminate unique values (4, 8 in this
Here's my version of the solution:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList randomNumbers = new ArrayList();
ArrayList expandingPlace = new ArrayList();
ArrayList sequenceOfDuplicates = new ArrayList();
for (int i = 0; i < 100; i++) {
randomNumbers.add((int) (Math.random() * 10));
expandingPlace.add(randomNumbers.get(i));
}
System.out.println(randomNumbers); // Original list.
for (int i = 0; i < randomNumbers.size(); i++) {
if (expandingPlace.get(i) == expandingPlace.get(i + 1)) {
expandingPlace.add(0);
sequenceOfDuplicates.add(expandingPlace.get(i));
sequenceOfDuplicates.add(expandingPlace.get(i + 1));
}
}
System.out.println(sequenceOfDuplicates); // What was in duplicate there.
}
}
It adds numbers from 0 to 9 to a list, and it adds to another list what is in "duplicate" (a number followed by the same number). You can use your big list instead of my randomNumbers ArrayList.