使用简单的 for 循环查找模式(数组中最常见的值)?

发布于 2024-11-30 20:21:02 字数 309 浏览 2 评论 0原文

如何使用简单的 for 循环找到众数(数组中最常见的值)?

该代码编译后输出错误。

这是我所拥有的:

public static void mode(double [] arr)
{
    double mode=arr[0];

    for(int i = 1; i<arr.length; i++)
    {   
        if(mode==arr[i])
        {
            mode++;
        }

     }


    return mode;
}

How do I find the mode (most frequent value in an array) using a simple for loop?

The code compiles with a wrong output.

Here is what I have:

public static void mode(double [] arr)
{
    double mode=arr[0];

    for(int i = 1; i<arr.length; i++)
    {   
        if(mode==arr[i])
        {
            mode++;
        }

     }


    return mode;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

太阳公公是暖光 2024-12-07 20:21:02

首先,我按顺序对数组进行排序,然后计算一个数字的出现次数。仅适用于循环和 if 语句,没有哈希图。

我的代码:

static int Mode(int[] n){
    int t = 0;
    for(int i=0; i<n.length; i++){
        for(int j=1; j<n.length-i; j++){
            if(n[j-1] > n[j]){
                t = n[j-1];
                n[j-1] = n[j];
                n[j] = t;
            }
        }
    }

    int mode = n[0];
    int temp = 1;
    int temp2 = 1;
    for(int i=1;i<n.length;i++){
        if(n[i-1] == n[i]){
            temp++;
        }
        else {
            temp = 1;
        }
        if(temp >= temp2){
            mode = n[i];
            temp2 = temp;
        }
    }
    return mode;
}

First I sort the array by order and then I count occurrences of one number. No hashmaps only for loop and if statements.

My code:

static int Mode(int[] n){
    int t = 0;
    for(int i=0; i<n.length; i++){
        for(int j=1; j<n.length-i; j++){
            if(n[j-1] > n[j]){
                t = n[j-1];
                n[j-1] = n[j];
                n[j] = t;
            }
        }
    }

    int mode = n[0];
    int temp = 1;
    int temp2 = 1;
    for(int i=1;i<n.length;i++){
        if(n[i-1] == n[i]){
            temp++;
        }
        else {
            temp = 1;
        }
        if(temp >= temp2){
            mode = n[i];
            temp2 = temp;
        }
    }
    return mode;
}
眼泪都笑了 2024-12-07 20:21:02

- 只需使用一个 HashMap,其中包含数组索引值作为键,其出现次数作为值。

- 在遍历 for 循环时通过检查当前索引是否已存在于 HashMap 中来更新 HashMap。如果确实如此,则在哈希映射中找到该双精度,并查看它已经发生了多少次,然后将其放回哈希映射中,并再出现一次。

-我是用Java做的,因为这就是你正在使用的。同样好的一点是,时间复杂度为 O(n),这是这种情况下您可能获得的最佳时间复杂度,因为您必须至少访问每个元素一次。

-所以如果你有一个像这样的双精度数组: { 1,2,3,1,1,1,5,5,5,7,7,7,7,7,7,7,7,7}
那么哈希映射最后看起来像这样: { 1->4, 2->1, 3->1, 5->3, 7->9 }
意思是“1出现4次,2出现1次......7出现9次”等等。

    public static double mode(double [] arr)
    {
        HashMap arrayVals = new HashMap();
        int maxOccurences = 1;
        double mode = arr[0];

        for(int i = 0; i<arr.length; i++)
        {   
            double currentIndexVal = arr[i];
            if(arrayVals.containsKey(currentIndexVal)){
                int currentOccurencesNum = (Integer) arrayVals.get(currentIndexVal);
                currentOccurencesNum++;
                arrayVals.put(currentIndexVal, currentOccurencesNum );
                if(currentOccurencesNum >= maxOccurences)
                {
                    mode = currentIndexVal;
                    maxOccurences = currentOccurencesNum;
                }
            }
            else{
                arrayVals.put(arr[i], 1);
            }
        }


        return mode;
    }

-Just use a HashMap which contains the array index values as the keys and their occurrence numbers as the values.

-Update the HashMap as you traverse the for loop by checking to see if the current index already exists in the HashMap. IF IT DOES then find that double in the hash map and see how many times it has already occurred and put it back in the HashMap with one more occurrence.

-I did it in Java because that's what it looks like you are using. What's also good is that the time complexity is O(n) which is the best you could possibly get for this type of scenario because you have to visit every element at least once.

-So if you have an array like this of doubles: { 1,2,3,1,1,1,5,5,5,7,7,7,7,7,7,7,7,7}
Then the hash map will look something like this at the end: { 1->4, 2->1, 3->1, 5->3, 7->9 }
Meaning that "1 occurred 4 times, 2 occured 1 time .... 7 occurred 9 times" etc.

    public static double mode(double [] arr)
    {
        HashMap arrayVals = new HashMap();
        int maxOccurences = 1;
        double mode = arr[0];

        for(int i = 0; i<arr.length; i++)
        {   
            double currentIndexVal = arr[i];
            if(arrayVals.containsKey(currentIndexVal)){
                int currentOccurencesNum = (Integer) arrayVals.get(currentIndexVal);
                currentOccurencesNum++;
                arrayVals.put(currentIndexVal, currentOccurencesNum );
                if(currentOccurencesNum >= maxOccurences)
                {
                    mode = currentIndexVal;
                    maxOccurences = currentOccurencesNum;
                }
            }
            else{
                arrayVals.put(arr[i], 1);
            }
        }


        return mode;
    }
屋檐 2024-12-07 20:21:02

此代码是不使用哈希图的不同方式。该方法是在 java 中创建的,采用一个数组作为参数,并在方法内创建另一个名为“numberCount”的数组。该数组“numberCount”将其索引设置为数组中的值。包含传递数组中的值的“numberCount”索引会将“numberCount”的值加 1(“++numberCount[array[i]]”),然后转到数组中的下一个值(重复直到数组的末尾)。然后创建另一个 for 循环来遍历“numberCount”中数组的每个值,具有最高值/计数的索引将被存储并作为“max”返回。此方法必须经过一些困难的更改才能使用双数组。但似乎与 int 数组配合得很好。

public static int findMostFrequentValue(int[] array) {
    int i;
    int[] numberCount = new int[100];
    for (i = 0; i < array.length; i++)++numberCount[array[i]];
    int max = 0;
    int j;

    for (j = 0; j < numberCount.length; j++) {
        if (numberCount[j] > max) max = j;
    }
    return max;
}

This code is a different way that does not use hashmaps. This method, created in java, takes an array as the parameter and creates another array called "numberCount" within the method. This array "numberCount" will set its index to the value in the array. The index of "numberCount"that contains the value in the array passed will add 1 to the value of "numberCount" ("++numberCount[array[i]]") then will go to the next value in the array (repeat until the end of the array). Then creates another for loop to go through each value of the array in "numberCount", which ever index has the highest value/count will be stored and return as "max." This method will have to undergo some difficult changes to use a double array. but seems to work great with an int array.

public static int findMostFrequentValue(int[] array) {
    int i;
    int[] numberCount = new int[100];
    for (i = 0; i < array.length; i++)++numberCount[array[i]];
    int max = 0;
    int j;

    for (j = 0; j < numberCount.length; j++) {
        if (numberCount[j] > max) max = j;
    }
    return max;
}
红玫瑰 2024-12-07 20:21:02

您应该检查数组中每个元素出现的次数。您可以通过 2 个内部 for 循环将数组的每个元素与自己和其他元素进行比较来做到这一点。

请记住,如果数组未排序并且包含超过 1 个模态值(因此重复出现的次数),则这将返回第一个模态值。首先通过Arrays.sort(array)对数组进行排序可能是明智的,这样您就可以选择最小或最大的模态值。

public static int modeOfArray(int[] array){
    int mode;     
    int maxOccurance = 0;

    for(int i=0; i<array.length; i++){
        int occuranceOfThisValue = 0;
        for(int j=0; j<array.length; j++){
            if(array[i] == array[j])
               occuranceOfThisValue++;
        }

        if(occuranceOfThisValue > maxOccurance){
            maxOccurance = occuranceOfThisValue;
            mode = array[i];
        }
    }
    return mode;
}

You should check the number of occurances of every element in your array. You can do it by comparing every element of array with herself and others via 2 inner for loops.

Remember, if the array is not sorted and contains more then 1 modal value (thus repeating number of occurances) this will return the first one. It maybe wise to order the array first by Arrays.sort(array) so that you can pick the smallest or biggest modal value.

public static int modeOfArray(int[] array){
    int mode;     
    int maxOccurance = 0;

    for(int i=0; i<array.length; i++){
        int occuranceOfThisValue = 0;
        for(int j=0; j<array.length; j++){
            if(array[i] == array[j])
               occuranceOfThisValue++;
        }

        if(occuranceOfThisValue > maxOccurance){
            maxOccurance = occuranceOfThisValue;
            mode = array[i];
        }
    }
    return mode;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文