Use currentIndex to store the first index of consecutive items.
Use i to iterate through the array.
if i.item != currentIndex.item
then set the item of currentIndex.next to i.item
and currentIndex++
After one iteration, the currentIndex will become the last meanful item in the new array.
then just set every item after currentIndex to -1.(in LinkedList version, just need to set currentIndex.next to null)
The code look like this:
int currentIndex = 0;
for (int i = 1; i < ints.length; i++) {
if (ints[currentIndex] != ints[i]) {
ints[currentIndex + 1] = ints[i];
currentIndex++;
}
}
for (int i = currentIndex + 1; i < ints.length; i++) {