如何在数组中找到最重复的值?

时间:2017-02-16 01:10:40

标签: c arrays vector

我正在尝试找到我的向量中最重复的int数。 这是我的代码......

for(i=0; i < dim; i++){
        temp=vet[i];
    for(i=0; i < dim; i++){
        if(vet[i]==temp){
            count++;
        }
    }
    if(most < count){
        most=count;
        elem=vet[i];
    }
}
return elem;}

这不正确..我希望你能帮助我..谢谢!

3 个答案:

答案 0 :(得分:2)

最明显的问题是您的代码在内部循环和外部循环中都使用imostcount变量在上面的代码中未初始化,每次启动内循环之前都需要重置count

此代码中使用的方法迭代整个数组,以便为​​每个元素计数外观,效率不高。通过从i + 1开始内循环而不是从0开始,可以提高效率。通过这样做,每个元素的第一个频率计数将是正确的,尽管后面的计数将是低的,因为不会访问先前的索引。但这没关系,因为如果可能的话,第一个计数将设置most变量。在内循环开始之前,count变量可以设置为1,因为i元素是测试值,内循环将跳过此索引。此更改将大大减少阵列访问次数。

请注意,此函数将返回数组中 first 元素的值,该元素也是最常出现的元素。

int get_most_common(int vet[], size_t dim)
{
    size_t i, j, count;
    size_t most = 0;
    int temp, elem;

    for(i = 0; i < dim; i++) {
        temp = vet[i];
        count = 1;
        for(j = i + 1; j < dim; j++) {
            if(vet[j] == temp) {
                count++;
            }
        }
        if (most < count) {
            most = count;
            elem = vet[i];
        }
    }
    return elem;
}

答案 1 :(得分:0)

您可以随时尝试强力方法,计算每个元素的频率,然后找到最大值。

要有效地实现此类功能的完整版本,您需要特殊的数据结构,例如hashtabledictionary

但是如果您只需要返回符合此条件的第一个项目,则以下代码可以正常工作。

#include <stdio.h>

// returns the first matched most frequent item of a list
// list items of same value must be grouped, for example, a sorted list
// only returns the first matched item
int max_frequent_item_of(int vet[], int dim)
{
  int i = 0;
  // element and its count of current sublist
  int current = vet[0];
  int current_count = 0;
  // most frequent element and its count of the list so far
  int most = vet[0];
  int most_count = 0;
  while (i < dim) {
    // if i-th element still belong to current sublist, run the counter
    if (vet[i] == current) {
      current_count += 1;
      i += 1;
    }
    // otherwise, the current sublist just ended
    else {
      // update the most frequent element
      if (current_count > most_count) {
        most = current;
        most_count = current_count;
      }
      // reset current sublist
      current = vet[i];
      current_count = 1;
      i += 1;
    }
  }
  printf("most frequent item %d, count %d\n", most, most_count);
  return most;
}

int main(void)
{
  // vet must be in order, sort with `qsort` if necessary
  int vet[] = {1, 1, 2, 3, 4, 4, 4, 8, 9, 9};
  int size = 10;
  int n;
  printf("list: ");
  for (n = 0 ; n < size; n++)
    {
      printf("%d ", vet[n]);
    }
  printf("\n");
  max_frequent_item_of(vet, size);
  return 0;
}

输出

list: 1 1 2 3 4 4 4 8 9 9
most frequent item 4, count 3

答案 2 :(得分:0)

我编写此代码可以满足您的需要,如果两个数字的重复时间相同,它会返回更大的一个。

     int ReturnMostRepeated(int val[], size_t dim)
{

size_t i,j,count;



int temp=0, temp2;





for(i =0;i<dim;i++)
        {


count = 1;

        for(j=i+1;j<dim;j++)
            {


if(val[j]==val[i])
            {
                count++;
            }

            }


if (count>temp)
        {
            temp = count;
            temp2 = val[i];
        }else if(count == temp) if (val[i]>temp2) temp2 = val[i];

        }

        return temp2;

    }