Program opened below. You can scroll through the algorithm, Java code and output.
Program 15
Remove Duplicate Array Elements In Place
Question
Read ten integers and remove all later duplicate occurrences without using another array. Preserve first-occurrence order.
Algorithm
Step 1: Start and declare a ten-element integer array.
Step 2: Accept all ten values from the user.
Step 3: Set the effective array size to ten.
Step 4: Select the first element as the current reference.
Step 5: Position another pointer immediately after the reference.
Step 6: Compare the two indicated elements.
Step 7: If the elements differ, advance the second pointer.
Step 8: If they match, start a left-shift at the duplicate position.
Step 9: Move each later element one position to the left.
Step 10: Reduce the effective size by one.
Step 11: Retest the current position because another duplicate may have shifted into it.
Step 12: Continue comparisons through the effective end.
Step 13: Advance the reference pointer to the next retained element.
Step 14: Repeat until every retained element has been examined.
Step 15: Print the array through the final effective size.
Step 16: Stop.
Java program
Javaimport java.util.*;
class ArrayUnique {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int[10];
for (int i = 0; i < 10; i++) {
a[i] = sc.nextInt();
}
int size = 10;
for (int i = 0; i < size; i++) {
int j = i + 1;
while (j < size) {
if (a[i] == a[j]) {
for (int k = j; k < size - 1; k++) {
a[k] = a[k + 1];
}
size--;
} else {
j++;
}
}
}
for (int i = 0; i < size; i++) {
System.out.print(a[i] + " ");
}
}
}Sample output
Input: 4 3 2 4 2 4 4 2 2 2
Output: 4 3 2