简体   繁体   English

如何在数组中找到第二大数字,但返回该值出现的最后一个索引?

[英]How to find the 2nd largest number in the array, but return the last index that the value appears in?

My actual problem is to find the number in a data set with the 2nd highest frequency. 我的实际问题是找到频率第二高的数据集中的数字。 I'm initializing an array that is the size of the largest number in the data set and then incrementing the corresponding indexes in the array each time the number appears in the data set. 我正在初始化一个数组,该数组是数据集中最大数字的大小,然后在数据出现在数据集中时递增数组中的相应索引。 If more than two indexes shares the 2nd highest frequency then I need to return the larger index. 如果两个以上的索引共享第二高的频率,那么我需要返回更大的索引。 My code returns the correct answer in some cases, but not all and I am having trouble finding the error in my logic. 在某些情况下,我的代码会返回正确的答案,但不是全部,我在逻辑中找不到错误。

int secondFreq(int data[], int maxNum){
    int highest = 0;
    int secondHighest = 0;
    int x;

    for(x = 0; x < maxNum; x++){
        if(data[x] > data[highest]){
            secondHighest = highest;
            highest = x;
        }
        else if (data[x] > data[secondHighest]){
            secondHighest = x;
        }
        else if (data[x] == data[secondHighest]){
            secondHighest = x;
        }
    }

    return secondHighest + 1;
}

Here is an example of an array that yields the wrong answer. 以下是一个产生错误答案的数组示例。 The left number is the index and the right number is the value stored at that index. 左边的数字是索引,右边的数字是存储在该索引中的值。 My function returns 12 (11th index+1), but it should be returning 5 (4th index + 1). 我的函数返回12(第11个索引+ 1),但它应该返回5(第4个索引+ 1)。

    0 - 2
    1 - 2
    2 - 5
    3 - 2
    4 - 5
    5 - 4
    6 - 2
    7 - 2
    8 - 6
    9 - 4
    10 - 3
    11 - 6
    12 - 2
    13 - 2
    14 - 3

In your example, the second '6'(index 11) will go to the 'else if (data[x] > data[secondHighest])' and update the 'secondHighest' to be the same as 'highest'. 在您的示例中,第二个'6'(索引11)将转到'else if(data [x]> data [secondHighest])'并将'secondHighest'更新为与'highest'相同。 This logic cannot deal with the condition where there are multiple highest value. 该逻辑不能处理具有多个最高值的条件。 To fix, you can put a 'else if(data[x] == data[highest])' before other 'else if'. 要修复,你可以在其他'else if'之前添加'else if(data [x] == data [highest])'。

However, you will get another incorrect anwser if your data set is as below: 但是,如果您的数据集如下,您将获得另一个不正确的anwser:

0 - 2
1 - 2
2 - 5

I will change the code to be more clear and readable as below: 我将更改代码更清晰,更可读,如下所示:

int secondFreq(int data[], int maxNum){
    int secondLargestValue = findsecondLargestValue(data);
    return findIndexofValue(data, secondLargestValue) + 1;
}

int findsecondLargestValue(int data) {
    ...
}

int findIndexofValue(int data, int value) {
    ...
}

Were it not for the efficiency consideration of being 2 pass, (so this is not going to be the best answer), I would want to say something like: 如果不考虑2次通过的效率考虑,(所以这不是最好的答案),我想说的是:

int secondFreq(int data[], int maxNum)
{
    double          highestVal = data[0];   // start with the first
                        // entry
    int             secondHighest = -1; // will send back 0 if everything
                    // ties for first
    int             x;      // for loop index

    for (x = 1; x < maxNum; x++) {
    if (data[x] > highestVal) {
        highestVal = data[x];
    }
    };
    for (x = 1; x < maxNum; x++) {
    if (data[x] != highestVal)
        &&((secondHighest == -1) || (data[x] >= data[secondHighest])) {
        secondHighest = x;
        }
    }


    return secondHighest + 1;
}

It is harder to mess up, though it does look through twice. 虽然它看起来经历了两次,但它更难搞乱。

Your code has two problems: 您的代码有两个问题:

  1. You will need to set highest = x; 你需要设置highest = x; in the case of data[x] == data[highest] . data[x] == data[highest]
  2. secondHighest should be initially pending. secondHighest最初应该是待定的。

The following is an example modification: 以下是一个示例修改:

int secondFreq(int data[], int maxNum){
    int highest = 0;
    int secondHighest, secondFlag = 0;//secondFlag : Whether secondHighest has been determined, 0 : undetermined, 1(Not 0) : determined
    int x;

    for(x = 1; x < maxNum; x++){
        if(data[x] > data[highest]){
            secondHighest = highest;
            highest = x;
            secondFlag = 1;
        }
        else if (data[x] == data[highest]){
            highest = x;
        }
        else if (secondFlag == 0 || data[x] >= data[secondHighest]){
            secondHighest = x;
            secondFlag = 1;
        }
    }

    if(secondFlag)
        return secondHighest + 1;
    else
        return 0;//All elements same
}

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

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