I am working on the level 1 of the Google FooBar Minion scheduling challenge:
Write a function called
solution(data, n)that takes in a list of less than100integers and a numbern, and returns that same list but with all of the numbers that occur more thanntimes removed entirely.The returned list should retain the same ordering as the original list - you don't want to mix up those carefully-planned shift rotations!
For instance, if data was
[5, 10, 15, 10, 7]andnwas1,solution(data, n)would return the list[5, 15, 7]because10occurs twice, and thus was removed from the list entirely.
This is my solution, but for some reason only 2 test cases are passing out of 10.
The strange thing is, the open test case {1, 2, 2, 3, 3, 3, 4, 5, 5}, n = 1 is also failing, while in my local IDE it passes.
public static int[] solution(int[] data, int n) {
Map<Integer, Integer> map = new LinkedHashMap<>();
if (data.length < 1) {
return data;
}
if (n < 1) {
return new int[0];
}
for (final int datum : data) {
map.put(datum, map.getOrDefault(datum, 0) + 1);
}
List<Integer> t = map.entrySet()
.stream()
.filter(x -> x.getValue() == 1)
.map(Map.Entry::getKey)
.toList();
int[] b = new int[t.size()];
for (int i = 0; i < t.size(); i++) {
b[i] = t.get(i);
}
return b;
}