Question: Use a one-dimensional array to solve the following problem: Read in 10 numbers, each of which is between 10 and 100. As each number is
Use a one-dimensional array to solve the following problem: Read in 10 numbers, each of which is between 10 and 100. As each number is read, print it only if it's not a duplicate of a number that has already been read. Provide for the "worst case", in which all 10 numbers are different. Use the smallest possible array to solve this problem. This needs to be in Java and Html. I really haven't gone through the code to see what's wrong, I am a little behind with other things. Please help, I need it soon!
public class Duplicate
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int[] array = new int[10];
System.out.println("Please enter the numbers one by one below.");
// looping 10 times to input numbers one by one
for(int i = 0; i < array.length; i++)
{
int num = input.nextInt();
// if block to check whether number is unique or not
// if not duplicate then print the message
if(!isDuplicate(array, num, i))
System.out.println(num + " is not a duplicate number.");
array[i] = num;
}
}
/**
* Method to check whether a number exist in the array or not, passed as parameter
* @param array[]
* @param num to check
* @param size of array
* @return boolean
*/
public static boolean isDuplicate(int array[], int num, int size)
{
for(int i = 0; i < array.length; i++)
{
// checking whether num is equal to array[i]
// if yes then return true
if(num == array[i])
return true;
}
return false;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
