删除给定数组中n次超过N的所有数字

发布于 2025-02-05 02:51:40 字数 1288 浏览 1 评论 0原文

我正在研究Google Foobar Minion计划挑战的1级:

编写一个称为解决方案(数据,n)的函数,该函数列出了少的列表 比100整数和一个数字n,并返回相同的列表 出现的所有数字比n times 完全删除完全。

返回的列表应保留与原始列表相同的订购 - 您不想混合那些精心计划的轮班旋转!

实例,如果数据是[5、10、15、10、7]n1solution>解决方案(数据,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
than 100 integers and a number n, and returns that same list but with
all of the numbers that occur more than n 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] and n was 1, solution(data, n) would return the list [5, 15, 7] because 10 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2025-02-12 02:51:40

返回相同的列表,但随着所有发生的数字发生更多
n times 完全删除

返回的列表应保留相同的订购

按照原始问题说明保留相同的订购,您只需要丢弃比n的事件数量的元素,而没有其他事件。

没有要求,将结果数组中的所有元素都不同。这意味着您必须保留所有将遇到的重复元素n或小于n times。

构建构建频率直方图的方法(计数出现的数量)是正确的,但是您不需要linkedhashmap

可以使用Java 8方法merge()

public static Map<Integer, Integer> countFrequencies(int[] arr) {
    Map<Integer, Integer> frequencies = new HashMap<>();
    for (int next: arr) {
        frequencies.merge(next, 1, Integer::sum);
    }
    return frequencies;
}

或使用流API:

public static Map<Integer, Long> countFrequencies(int[] arr) {
    
    return Arrays.stream(arr)
        .boxed()
        .collect(Collectors.groupingBy(
            Function.identity(),
            Collectors.counting()));
}

确保顺序 ,您只需要迭代给定的数组,然后滤除具有频率 的元素, 等于 n

另外,无需创建中间列表,您可以通过执行流管线而获得产生的数组

public static int[] solution(int[] data, int n) {
    
    Map<Integer, Long> frequencies = countFrequencies(data);
    
    return Arrays.stream(data)
        .filter(num -> frequencies.get(num) <= n)
        .toArray();
}

请参阅 在线演示

也确保您还拥有所有< strong>导入在您提交的文件顶部(上面给出的代码导入在emo 中列出)。

并且不要使用导入java.util。测试 。

returns that same list but with all of the numbers that occur more
than n times removed entirely.

The returned list should retain the same ordering

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 than n 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():

public static Map<Integer, Integer> countFrequencies(int[] arr) {
    Map<Integer, Integer> frequencies = new HashMap<>();
    for (int next: arr) {
        frequencies.merge(next, 1, Integer::sum);
    }
    return frequencies;
}

Or with Stream API:

public static Map<Integer, Long> countFrequencies(int[] arr) {
    
    return Arrays.stream(arr)
        .boxed()
        .collect(Collectors.groupingBy(
            Function.identity(),
            Collectors.counting()));
}

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.

public static int[] solution(int[] data, int n) {
    
    Map<Integer, Long> frequencies = countFrequencies(data);
    
    return Arrays.stream(data)
        .filter(num -> frequencies.get(num) <= n)
        .toArray();
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文