Code:
int[] input = new int[]{0, 0, 0, 0, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 5, 5, 5, 5, 8, 8, 10, 10, 2, 2, 2, 3, 3, 7, 7};
int current = input[0];
boolean found = false;
for (int i = 1; i < input.length; i++) {
if (current == input[i] && !found) {
found = true;
} else if (current != input[i]) {
System.out.print(" " + current);
current = input[i];
found = false;
}
}
System.out.print(" " + current);
output:
0 2 3 5 8 10 2 3
Explanation:
you set current with first value of array after that you go through
the loop.Inside the loop, if the current value equal to each elements
and flag fount is false which mean duplicate have been not seen and
you it has been seen right now, so flag which is found variable has
been set to true which shows duplicate has been found or seen an do
nothing.For the else part, if the element of array not equal to current
variable means there is not duplicate, and right current variable that
contains the before seen duplicate at the console. Update the
variable current with new duplicate and set flag found to false which
shows have not seen new seen duplicate. Keep doing this and right last
duplicate variable when you are done traversing the loop.
4
solved Extraction of unique values form a array list