-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateFinder.java
More file actions
45 lines (37 loc) · 1.15 KB
/
Copy pathDuplicateFinder.java
File metadata and controls
45 lines (37 loc) · 1.15 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
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class DuplicateFinder {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(10, 20, 30, 40, 10, 60, 60, 80, 70, 10);
List<Integer> distinct = algorithm1(numbers);
System.out.println(distinct.size() == numbers.size());
Map<Integer, List<Integer>> collect = algorithm2(numbers);
System.out.println(collect);
long count = algorithm3(numbers);
System.out.println(count);
}
//Using collect(), filter()
private static long algorithm3(List<Integer> numbers) {
long count = numbers.stream()
.collect(Collectors.groupingBy(i -> i))
.values()
.stream()
.filter(l -> l.size() > 1)
.count();
return count;
}
//Using groupingBy()
private static Map<Integer, List<Integer>> algorithm2(List<Integer> numbers) {
return numbers.stream()
.collect(Collectors.groupingBy(i -> i));
}
//Using distinct()
private static List<Integer> algorithm1(List<Integer> numbers) {
List<Integer> distinct = numbers.stream()
.distinct()
.collect(Collectors.toList());
return distinct;
}
}