简体   繁体   English

如何使用哈希图在数组中查找最小模式

[英]how to use hashmap to find smallest mode in the array

The question is that given an array returns the most frequently occurring element of an array of positive integers. 问题是给定一个数组返回正整数数组中最频繁出现的元素。 Assume that the array has at least one element .If there are more than one mode return the smallest mode. 假设数组中至少有一个元素。如果有多个模式,则返回最小模式。

Here is my code, but it can not pass this array {27, 15, 15, 11, 27}. 这是我的代码,但无法传递此数组{27,15,15,15,11,27}。

Also,how to modify my code without using i in for-each loop? 另外,如何在for-each循环中不使用i情况下修改我的代码?

    public int mode(int input[]){
    if(input.length==1)
        return input[0];
    Map<Integer,Integer> mymap = new HashMap<Integer,Integer>();
    int count=0;
    for(int i = 0 ;i < input.length;i++){
        count = mymap.containsKey(input[i]) ? mymap.get(input[i]) : 0;
         mymap.put(input[i],count+1);
    }
    int firstmode = -1;
    int secondmode = -1;
    int i=0;
    for(int k :mymap.keySet()){
        if(mymap.get(k)>secondmode){
            i++;
            firstmode=k;
            if(i%2==0){ //if there more than one mode,then compare them
                if(firstmode>k){
                   firstmode=k;
                   i--;
                }
            }
        secondmode=mymap.get(k);
        }  
    }
    return firstmode;
}

Updated test cases 更新的测试用例

{1, 1, 2, 3}    should return 1 
{1, 1, 2, 3, 3} should return 1
{27, 15, 15, 11, 27}    should return 15    
{27, 15, 15, 11, 27, 27}    should return 27    
{1, 1, 6, 6, 6, 3, 3, 3}    should return 3 
{27, 15, 15, 27, 11, 11, 11, 14, 15, 15, 16, 19, 99, 100, 0, 27}    
should return 15    
{42}    should return 42

I found your logic pretty unclear and convoluted so I just rewrote the code from scratch. 我发现您的逻辑非常不清楚且令人费解,所以我只是从头开始重写了代码。 I included a solution pre-Java 8 and another one post-Java 8. 我提供了Java 8之前的解决方案和Java 8之后的另一个解决方案。

import java.util.HashMap;
import java.util.Map;
import java.util.IntStream;

import static java.util.Comparator.naturalOrder;

public class Solution {
    public static int smallestMode(int input[]) {
        Map<Integer, Integer> counters = new HashMap<>();
        for (Integer i : input) {
            Integer counter = counters.get(i);
            counters.put(i, counter == null ? 1 : counter + 1);
        }

        int mode = Integer.MIN_VALUE;
        for (int counter : counters.values()) mode = Math.max(mode, counter);

        int result = Integer.MAX_VALUE;
        for (Map.Entry<Integer, Integer> entry : counters.entrySet()) {
            if (entry.getValue() == mode) result = Math.min(result, entry.getKey());
        }

        return result;
    }

    public static int smallestModeJava8(int input[]) {
        Map<Integer, Long> counters = IntStream.of(input).boxed().collect(groupingBy(identity(), counting()));
        long mode = counters.values().stream().max(naturalOrder()).get();
        return counters.entrySet().stream().filter(entry -> entry.getValue() == mode).map(Map.Entry::getKey).min(naturalOrder()).get();
    }

    public static void main(String[] args) {
        int[] input = {27, 15, 15, 11, 27};
        System.out.println(smallestMode(input));
        System.out.println(smallestModeJava8(input));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM