Design and implement a Java program for programming exercise
Solution
Please follow the code and comments for description :
CODE :
import java.util.Scanner;
public class EliminateDuplicates { // class to run the code
public static int[] removeDuplicates(int[] list) { // method to remove the duplicates and return the data
int endData = list.length;
for (int i = 0; i < endData; i++) { // iterate over the data array
for (int j = i + 1; j < endData; j++) {
if (list[i] == list[j]) { // check for the data if equal
int leftShift = j; // shift the data
for (int k = j + 1; k < endData; k++, leftShift++) { // check for the next element
list[leftShift] = list[k];
}
endData--; // decrement the index
j--;
}
}
}
int[] resData = new int[endData]; // save the data to a list
for (int i = 0; i < endData; i++) {
resData[i] = list[i];
}
return resData; // return the data
}
public static void main(String[] args) { // driver method
Scanner sc = new Scanner(System.in); // scanner class to get the data
int arrData[] = new int[10]; // local varaibles
int resultData[] = new int[10];
System.out.print(\"Enter Ten Numbers : \"); // prompt for the user
String str = sc.nextLine();
String[] array = str.split(\" \"); // split the data
for (int i = 0; i < array.length; i++) {
arrData[i] = Integer.parseInt(array[i]); // save as integer
}
resultData = removeDuplicates(arrData); // call the method to remove the duplicates
System.out.print(\"The Distinct Numbers are : \");
for (int j = 0; j < resultData.length; j++) {
System.out.print(resultData[j]);
System.out.print(\" \"); // print the data to console
}
System.out.println(\"\"); // new line character
}
}
OUTPUT :
Enter Ten Numbers : 1 2 3 2 1 6 3 4 5 2
The Distinct Numbers are : 1 2 3 6 4 5
Hope this is helpful.

