删除给定数组中n次超过N的所有数字
我正在研究Google Foobar Minion计划挑战的1级:
编写一个称为
。解决方案(数据,n)
的函数,该函数列出了少的列表 比100
整数和一个数字n
,并返回相同的列表 出现的所有数字比n
times 完全删除完全。返回的列表应保留与原始列表相同的订购 - 您不想混合那些精心计划的轮班旋转!
实例,如果数据是
[5、10、15、10、7]
和n
是1
,solution>解决方案(数据,n )
将返回列表[5,15,7]
因为10
发生了两次,因此 被完全从列表中删除。
这是我的解决方案,但由于某些原因,只有2个测试用例在10中传递
。 ,n = 1
也失败了,而在我的本地IDE中,它通过。
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;
}
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
than100
integers and a numbern
, and returns that same list but with
all of the numbers that occur more thann
times 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]
andn
was1
,solution(data, n)
would return the list[5, 15, 7]
because10
occurs 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按照原始问题说明保留相同的订购,您只需要丢弃比
n
的事件数量的元素,而没有其他事件。没有要求,将结果数组中的所有元素都不同。这意味着您必须保留所有将遇到的重复元素
n
或小于n
times。构建构建频率直方图的方法(计数出现的数量)是正确的,但是您不需要
linkedhashmap
。可以使用Java 8方法
merge()
:或使用流API:
确保顺序 ,您只需要迭代给定的数组,然后滤除具有频率 的元素, 等于
n
。另外,无需创建中间列表,您可以通过执行流管线而获得产生的数组。
请参阅 在线演示
也确保您还拥有所有< strong>导入在您提交的文件顶部(上面给出的代码导入在emo 中列出)。
并且不要使用
导入java.util。测试 。
According to the original problem statement, you need to discard only elements having the number of occurrences more than
n
, and nothing else.There's no requirement that all elements in the resulting array should be distinct. It means you have to preserve all duplicated elements that will be encountered
n
or less thann
times.The approach of build building a histogram of frequencies (counting the number of occurrences) is correct, but you don't need
LinkedHashMap
.This logic can be written like that using Java 8 method
merge()
:Or with Stream API:
To ensure the order, you just need to iterate over the given array, and filter out the element having frequency less or equal to
n
.Also, there's no need to create an intermediate list, you can get the resulting array as a result of execution of the stream pipeline.
See the Online Demo
Also make sure that you have all the imports at the top of the file you're submitting (imports for the code given above are listed in the demo).
And don't use
import java.util.*
, because of that your solution might not pass the tests.