Hey guys I\'ve been working on 3 other programs and when i got to this one it ju
ID: 3632203 • Letter: H
Question
Hey guys I've been working on 3 other programs and when i got to this one it just seemed like i kept getting errors when i compiled it. here's the question:Write a method to eliminate the duplicate values in the array using following method header:
public static int[] eliminateDuplicates(int[] numbers)
Write a test program that reads in ten integers, invokes the method, and displays the result. Here is the sample run of the program:
Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
The distinct numbers are: 1 2 3 6 4 5
*If you could reply with a source code that'd be amazing. thankyou.
Explanation / Answer
import java.util.*;
public class duplicate
{
public static void main(String[] args)
{
int[] array = new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("Enter ten numbers: ");
for (int i =0; i < 10; i++){
array[i] = sc.nextInt();
}
int[] noDup = eliminateDuplicate(array);
System.out.print("The distinct numbers are: ");
print(noDup);
}//end main
public static int[] eliminateDuplicate(int[] numbers){
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i <10; i++){
boolean dupCheck = true;
for(int j = 0; j< list.size(); j++){
if(numbers[i] == list.get(j)){
dupCheck = false;
}
}
if(dupCheck){
list.add(numbers[i]);
}
}
int[] fin = new int[list.size()];
for(int i =0; i<list.size(); i++){
fin[i] = list.get(i);
}
return fin;
} // end method
public static void print(int[] arr){
for(int i = 0; i < arr.length; i++){
System.out.printf("%d ", arr[i]);
}
}
}