In: Computer Science
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
input code:
output 1:
output 2:
code:
import java.util.*;
import java.io.*;
class Main {
/*make method for check distinct or not*/
static boolean Check(int input[])
{
/*check all element one by one*/
for (int i = 0; i < 10; i++)
{
/*check it is came ot not*/
int j;
for (j = 0; j < i; j++)
{
/*if came than return false*/
if (input[i] == input[j])
{
return false;
}
}
}
/*else return true*/
return true;
}
// Driver program
public static void main (String[] args)
{
/*declare the variables*/
int input[] = new int[10];
Scanner sc=new Scanner(System.in);
/*take input from user*/
System.out.print("Enter the values:");
for(int i=0;i<10;i++)
{
input[i]=sc.nextInt();
}
/*call function*/
boolean output=Check(input);
/*print outputs*/
if(output)
{
System.out.print("values are distinct.");
}
else
{
System.out.print("values are not distinct.");
}
}
}