Count Number of Distinct Values

Assignment

Solution

import java.util.Scanner;

public class Distinct {

public static void main(String args[]) {

int[] numbersArray=new int[10];

int j=0;

int p=1;

int c=0;

System.out.println(“Enter 10 numbers : “);

Scanner sc=new Scanner(System.in);

for(int i=0;i<10;i++){

j=sc.nextInt();

for(int k=0;k<p;k++){

if(numbersArray[k]==j){

break;

}

else if(k==p-1){

numbersArray[p-1]=j;

p++;

break;

}

}

}

for(int l=0;l<numbersArray.length;l++){

if(numbersArray[l]==0){

break;

}

else{

c++;}

}

System.out.println(“The number of distinct numbers is : “+c);

System.out.print(“The distinct numbers are : “);

for(int l=0;l<c;l++){

System.out.print(numbersArray[l]+” “);

}

}

}