-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElimduplicate.java
More file actions
37 lines (33 loc) · 1.08 KB
/
Elimduplicate.java
File metadata and controls
37 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
public class Elimduplicate {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Enter ten digits seperated by a space: ");
for (int i = 0; i < numbers.length; i++){
numbers[i] = input.nextInt();
}
int[] distinctNumbers = eliminateDuplicates(numbers);
System.out.println("Distinct numbers: ");
for (int num : distinctNumbers){
System.out.println(num);
}
}
public static int[] eliminateDuplicates(int[] arr){
Arrays.sort(arr);
int len = arr.length;
int[] temp = new int[len];
int j = 0;
for (int i = 0; i < len - 1; i++){
if (arr[i] != arr[i + 1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[len-1];
int[] result = new int[j];
for (int k = 0; k < j; k++) {
result[k] = temp[k];
}
return result;
}
}