Array: Print Distinct Numbers

Question: Write a program that reads in 10 numbers and displays the number of distinct numbers and the distinct numbers in their input order and separated by exactly one space

Example:

Original Array: {1,2, 3, 2, 1, 6, 3, 4, 5, 2}

Output: 1 2 3 6 4 5

```package Array;

/**
* Write a program that reads in 10 numbers
* and displays the number of distinct numbers and the distinct numbers in
* their input order and separated by exactly one space
* Created by aarushi on 9/5/2
*/
public class Ch7Ex5 {

public static void main (String [] args){
//create an array of 10 integers
int [] array= {1,2, 3, 2, 1, 6, 3, 4, 5, 2};
//second array stores the unique elements
int [] printElements= new int[array.length];
//number of elements in printElements
int array2Size=0;

for(int i=0; i<array.length; i++){
//shouldPrint decides whether an element should be printed
//it is initially set to true
boolean shouldPrint=true;
//run a loop through the length of printElements to check if the element has
for (int j=0; j<array2Size; j++){
if(array[i]==printElements[j]){
shouldPrint=false;
}
}
if (shouldPrint){
System.out.print(array[i] + " ");
//add the unique element to printElements and increase array2Size
printElements[array2Size++]=array[i];
}
}

}
}
```

This site uses Akismet to reduce spam. Learn how your comment data is processed.