You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用嵌套循环查找并筛选列表中重复次数符合指定要求的元素

解决按指定重复次数筛选列表元素的问题

Hey there! Let's work through fixing this problem so your program can filter elements based on their exact duplicate count.

First, let's break down the issue with your current nested loop: it only detects when an element appears more than once, but it doesn't track how many times each element shows up overall. That's why it can't tell if an element matches your numberOfDuplicates requirement. Plus, it'll print the same duplicate multiple times (like 4 would show up several times in your example), which isn't what you want.

方法1:用HashMap统计元素出现次数(基础易懂版)

The most straightforward way is to use a HashMap to count how many times each element appears. Here's how to implement it:

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DuplicateFilter {
    public static void main(String[] args) {
        List<Integer> integers = Arrays.asList(4,2,3,4,5,3,4,2,3);
        int numberOfDuplicates = 2; // 可以改成3来测试不同场景

        // 第一步:统计每个元素的出现次数
        Map<Integer, Integer> countMap = new HashMap<>();
        for (Integer num : integers) {
            // 如果元素已在map中,计数+1;否则初始化为1
            countMap.put(num, countMap.getOrDefault(num, 0) + 1);
        }

        // 第二步:筛选并打印符合重复次数要求的元素
        System.out.println("元素重复次数为" + numberOfDuplicates + "的有:");
        for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
            if (entry.getValue() == numberOfDuplicates) {
                System.out.println(entry.getKey());
            }
        }
    }
}

方法2:用Java 8 Stream API(简洁版)

If you're comfortable with Java 8+, you can use streams to make the code more concise and modern:

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DuplicateFilterStream {
    public static void main(String[] args) {
        List<Integer> integers = Arrays.asList(4,2,3,4,5,3,4,2,3);
        int numberOfDuplicates = 3;

        // 一行完成次数统计
        Map<Integer, Long> countMap = integers.stream()
                .collect(Collectors.groupingBy(num -> num, Collectors.counting()));

        // 筛选并打印结果
        System.out.println("元素重复次数为" + numberOfDuplicates + "的有:");
        countMap.entrySet().stream()
                .filter(entry -> entry.getValue() == numberOfDuplicates)
                .map(Map.Entry::getKey)
                .forEach(System.out::println);
    }
}

关于你提到的「计数器列表」思路

You mentioned thinking about a "counter list"—the problem with that approach is that lists are great for ordered data, but they're not efficient for looking up the count of a specific element. Using a HashMap (or any Map implementation) lets you directly map each element to its count, which makes lookup and comparison quick and intuitive.

Testing with your example list:

  • 4 appears 3 times
  • 2 appears 2 times
  • 3 appears 3 times
  • 5 appears 1 time

So when numberOfDuplicates = 2, only 2 gets printed. When numberOfDuplicates = 3, 4 and 3 get printed—exactly what you need!

内容的提问来源于stack exchange,提问作者Michał Jaroń

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:42:33