Java 数组中的多个最频繁元素

Java 数组中的多个最频繁元素,java,list,Java,List,假设我们有一个整数列表。我想检测并在屏幕上打印最频繁重复的项目。当最常见的元素只有一个时,我知道怎么做。但是,如果我们有一个包含以下元素的列表: 10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10 我想打印一个6和一个10,所以我的意思是我想打印所有最频繁重复的元素,不管有多少 nums = [10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10] def most_recurrent_ints(arr): log =

假设我们有一个整数列表。我想检测并在屏幕上打印最频繁重复的项目。当最常见的元素只有一个时,我知道怎么做。但是,如果我们有一个包含以下元素的列表:

10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10 
我想打印一个6和一个10,所以我的意思是我想打印所有最频繁重复的元素,不管有多少

nums = [10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10]

def most_recurrent_ints(arr):
    log = {}
    for i in arr:
        if i in log:
            log[i] += 1
        else:
            log[i] =  1
    current_max = 0
    for i in log.values():
        if i > current_max:
            current_max = i
    results = []
    for k, v in log.items():
        if v == current_max:
            results.append(k)
    return results
    
print(most_recurrent_ints(nums))

我不擅长Java,但这是Python解决方案,也许有人可以翻译。如果你需要,我可以用JS来做。

你考虑过使用字典类型的数据结构吗?我不是一个java人,所以这可能不好看,但它符合您的要求:

    final int[] array = new int[]{ 10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10 };

    Map<Integer, Integer> dictionary = new HashMap<Integer,Integer>(); 
    
    for(int i = 0; i < array.length; ++i){
        int val = array[i];
        if(dictionary.containsKey(val)){
            dictionary.put(val, dictionary.get(val) + 1);
        }else{
            dictionary.put(val, 1);
        }
    }

    for(Map.Entry<Integer,Integer> entry : dictionary.entrySet()){
        System.out.println(entry.getKey() + ": " + entry.getValue());
    }
您需要一个map(又名dictionary)数据结构来解决这个问题。这是我能想出的最快、最简洁的解决方案

public static void main(String[] args){
    int[] arr = new int[]{10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10};
    printMostFrequent(arr);
}
 
private static void printMostFrequent(int[] arr){
    // Key: number in input array
    // Value: amount of times that number appears in the input array
    Map<Integer, Integer> counts = new HashMap<>();

    // The most amount of times the same number appears in the input array. In this example, it's 4.
    int highestFrequency = 0;
    
    // Iterate through input array, populating map.
    for (int num : arr){
        
        // If number doesn't exist in map already, its frequency is 1. Otherwise, add 1 to its current frequency.
        int currFrequency = counts.getOrDefault(num, 0) + 1;
        
        // Update frequency of current number.
        counts.put(num, currFrequency);
        
        // If the current number has the highest frequency so far, store its frequency for later use.
        highestFrequency = Math.max(currFrequency, highestFrequency);
    }

    // Iterate through unique numbers in array (remember, a Map in Java allows no duplicate keys).
    for (int key : counts.keySet()){
        
        // If the current number has the highest frequency, then print it to console.
        if (counts.get(key) == highestFrequency){
            System.out.println(key);
        }
    }
}

你想打印多少个常用元素?我知道当最常用的元素只有一个时该怎么做——你能为你在问题中的尝试添加代码吗?
public static void main(String[] args) {
    MostFrequent(new Integer[] {10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10});
}

static void MostFrequent(Integer[] arr) {
    Map<Integer, Integer> count = new HashMap<Integer, Integer>();
    
    for (Integer element : arr) {
        if (!count.containsKey(element)) {
            count.put(element,0);
        }
        count.put(element, count.get(element) + 1);
    }

    Map.Entry<Integer, Integer> maxEntry = null;
    ArrayList<Integer> list = new ArrayList<Integer>();

    for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
        if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
            list.clear();
            list.add(entry.getKey());
            maxEntry = entry;
        }
        else if (entry.getValue() == maxEntry.getValue()) {
            list.add(entry.getKey());
        }
    }

    for (Integer item: list) {
        System.out.println(item); 
    }       
}
6
10
public static void main(String[] args){
    int[] arr = new int[]{10, 5, 2, 1, 2, 4, 6, 6, 6, 6, 10, 10, 10};
    printMostFrequent(arr);
}
 
private static void printMostFrequent(int[] arr){
    // Key: number in input array
    // Value: amount of times that number appears in the input array
    Map<Integer, Integer> counts = new HashMap<>();

    // The most amount of times the same number appears in the input array. In this example, it's 4.
    int highestFrequency = 0;
    
    // Iterate through input array, populating map.
    for (int num : arr){
        
        // If number doesn't exist in map already, its frequency is 1. Otherwise, add 1 to its current frequency.
        int currFrequency = counts.getOrDefault(num, 0) + 1;
        
        // Update frequency of current number.
        counts.put(num, currFrequency);
        
        // If the current number has the highest frequency so far, store its frequency for later use.
        highestFrequency = Math.max(currFrequency, highestFrequency);
    }

    // Iterate through unique numbers in array (remember, a Map in Java allows no duplicate keys).
    for (int key : counts.keySet()){
        
        // If the current number has the highest frequency, then print it to console.
        if (counts.get(key) == highestFrequency){
            System.out.println(key);
        }
    }
}
6
10