-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemoveDuplicates.java
More file actions
66 lines (58 loc) · 1.47 KB
/
Copy pathRemoveDuplicates.java
File metadata and controls
66 lines (58 loc) · 1.47 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package learnArrays;
import java.util.Arrays;
/*
* author: Koushik Chatterjee
* topic: Interview preparation
*/
public class RemoveDuplicates {
public static void main(String[] args) {
int[] input = new int[]{1, 1, 1, 3, 7, 7, 8, 6, 6, 90, 89, 9, 9, 10, 90,
90};
// usingStream(input);
System.out.println();
usingSort(input);
}
/*
* Fastest and easiest way using Java Stream in Collections
*/
static void usingStream(int[] arr) {
/*
* IntStream stream = Arrays.stream(arr); stream.distinct().forEach(i ->
* System.out.print(i + " "));
*/
Arrays.stream(arr).distinct().forEach(i -> System.out.print(i + " "));
}
private static void usingSort(int[] input) {
Arrays.sort(input);
int current = input[0];
for (int i = 0; i < input.length; i++) {
if (current != input[i]) {
System.out.print(current + " ");
current = input[i];
}
}
System.out.print(current);
/*
* int current = input[0]; for (int i = 0; i < input.length; i++) { if
* (current != input[i]) { System.out.print(current + " "); current =
* input[i]; } } System.out.print(current);
*/
}
/*
* not working
*/
static void remove(int[] arr) {
int length = arr.length;
int temp[] = new int[length];
for (int i = 0; i < length; i++) {
for (int j = 1; j < length; j++) {
if (arr[i] == arr[j]) {
temp[j] = arr[j];
}
}
}
for (int i : temp) {
System.out.println(i);
}
}
}