I am trying to get distinct List with unique int[], but I am still getting duplicates.
List<Integer> set1 = Arrays.asList(2, 3, 4, 5, 10, 4, 5, 6, 4);
List<Integer> count = new ArrayList<>();
List<int[]> combination = set1.stream().flatMap(i -> set1.stream().
flatMap(j -> set1.stream().map(k -> new int[] { i, j, k })))
.distinct().collect(Collectors.toList());
My logic to get required elements from the combined list of int[] is the numbers in array should be consecutive with difference of 1.
Ex: {2,3,4}
But in my final list the {2,3,4} is coming 3 times , it is obvious. I want to winnow the occurrences of 3 times {2,3,4} to 1. SO i wrote the below logic. Still there are many elements which are in consecutive form {4,5,6}, i want them also to be only once counted.
List<int[]> combination1 = combination.stream().distinct().collect(Collectors.toList());
combination1.stream().filter(i -> Collections.frequency(combination, i) == 1).forEach(s -> {
if (s[1] - s[0] == 1 && s[2] - s[1] == 1 && s[2] - s[0] == 2) {
count.add(1);
} else {
count.add(0);
}
});
System.out.println(count.stream().mapToInt(Integer::intValue).sum());
I am getting the unique elements as 15, but it should be 3.
My final
List<int[]>
should contain only : {2,3,4} , {3,4,5} , {4,5,6} and hence count should be three.