In: Computer Science
problem 1 (Duplicate Elimination) code in JAVA please
Use a one-dimensional array to solve the following problem: Write an application that inputs ten numbers, each between 10 and 100, both inclusive. Save each number that was read in an array that was initialized to a value of -1 for all elements. Assume a value of -1 indicates an array element is empty. You are then to process the array, and remove duplicate elements from the array containing the numbers you input. Display the contents of the array to demonstrate that the duplicate input values were actually removed.
Sample Output could be as follows:
Sample 1:
Please input a value in [10,100]
value 1: 78
value 2: 34
value 3: 46
value 4: 74
value 5: 87
value 6: 39
value 7: 39
value 8: 46
value 9: 78
value 10: 78
The unique values are:
78 34 46 74 87 39
The code in java is given below.
import java.util.Scanner; import static javafx.application.Platform.exit; public class duplicateElimination { public static void main(String[] args) { int[] arr=new int[10]; for(int i=0;i<10;i++) { arr[i]=-1; } System.out.println("Please input a value in [10,100]"); Scanner sc=new Scanner(System.in); int value; for(int i=0;i<10;i++) { System.out.print("value "+(i+1)+": "); value=sc.nextInt(); if(value<10 || value>100) { System.out.println("Value should be in [10,100]"); i=i-1; continue; } arr[i]=value; } System.out.println("The unique values are: "); int newLength=arr.length; for(int i=0;i<newLength;i++) { for(int j=i+1;j<newLength;j++) { if(arr[i]==arr[j]) { arr[j]=arr[newLength-1]; newLength--; j--; } } } for(int i=0;i<newLength;i++) { System.out.print(arr[i]+" "); } } } The screenshot of the running code and output is given below. If the answer helped please upvote it means a lot. For any query please comment.