用Java写一个mode方法,找出数组中出现频率最高的元素 [英] Write a mode method in Java to find the most frequently occurring element in an array

查看:42
本文介绍了用Java写一个mode方法,找出数组中出现频率最高的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:

编写一个名为 mode 的方法,该方法返回整数数组中出现频率最高的元素.假设数组至少有一个元素,并且数组中的每个元素都有一个介于 0 和 100 之间的值.通过选择较低的值打破平局.

Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value.

例如,如果传递的数组包含值 {27, 15, 15, 11, 27},则您的方法应返回 15.(提示:您可能希望查看本章前面的 Tally 程序以获得如何解决这个问题的想法.)

For example, if the array passed contains the values {27, 15, 15, 11, 27}, your method should return 15. (Hint: You may wish to look at the Tally program from earlier in this chapter to get an idea of how to solve this problem.)

下面是我的代码,除了单元素数组外几乎都可以工作

Below is my code that almost works except for single-element arrays

public static int mode(int[] n)
{
    Arrays.sort(n);
    
    int count2 = 0;
    int count1 = 0;
    int pupular1 =0;
    int popular2 =0;
    
    
    for (int i = 0; i < n.length; i++)
    {
            pupular1 = n[i];
            count1 = 0;    //see edit
        
        for (int j = i + 1; j < n.length; j++)
        {
            if (pupular1 == n[j]) count1++;
        }
        
        if (count1 > count2)
        {
                popular2 = pupular1;
                count2 = count1;
        }
        
        else if(count1 == count2)
        {
            popular2 = Math.min(popular2, pupular1);
        }
    }
    
    return popular2;
}

编辑:终于弄明白了.将 count1 = 0; 更改为 count1 = 1; 现在一切正常!

Edit: finally figured it out. Changed count1 = 0; to count1 = 1; everything works now!

推荐答案

对于此类问题,您应该使用哈希映射.将每个元素输入到哈希映射中需要 O(n) 时间,并且需要 o(1) 时间来检索元素.在给定的代码中,我基本上是采用全局最大值并将其与从哈希图中的get"接收到的值进行比较,每次我向其中输入一个元素时,看看:

You should use a hashmap for such problems. it will take O(n) time to enter each element into the hashmap and o(1) to retrieve the element. In the given code, I am basically taking a global max and comparing it with the value received on 'get' from the hashmap, each time I am entering an element into it, have a look:

hashmap 有两部分,一是键,二是对键进行 get 操作时的值,返回其值.

hashmap has two parts, one is the key, the second is the value when you do a get operation on the key, its value is returned.

public static int mode(int []array)
{
    HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
    int max  = 1;
    int temp = 0;

    for(int i = 0; i < array.length; i++) {

        if (hm.get(array[i]) != null) {

            int count = hm.get(array[i]);
            count++;
            hm.put(array[i], count);

            if(count > max) {
                max  = count;
                temp = array[i];
            }
        }

        else 
            hm.put(array[i],1);
    }
    return temp;
}

这篇关于用Java写一个mode方法,找出数组中出现频率最高的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆