C 中的重复项计数

标签 c arrays duplicates

如果我有一个数组设置为:

test[10] = {1,2,1,3,4,5,6,7,8,3,2}

我想返回数字 3,因为有重复的 1、重复的 2 和重复的 3。我该如何实现这一点?效率并不重要。

最佳答案

您可以使用此代码,

int main()
{
 int test[10] = {1,2,1,3,4,5,6,7,8,3,2};
 int i,j,dupliCount = 0;
 for (i =0; i<(sizeof(test)/sizeof(int));i++)
  {
  for(j=i+1;j<(sizeof(test)/sizeof(int));j++)
   {
    if (test[i] == test[j])
     {
      ++dupliCount;
      break;
     }
   }
  }
printf("duplicate count %d",dupliCount);
}

关于C 中的重复项计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15893015/

相关文章:

C++ 基础 : ranged based for-loop and passing C-style arrays to functions

r - 如何对组中最早日期的重复项进行子集化?

c - SPI EP93xx(带 Linux 的 TS7200 板)

java - 使用 netbeans gui builder 创建 GUI 组件数组

在c中将字符串转换为时间格式

c++ - 这个错误是什么意思 : variable-size type declared outside of any function

javascript - 在 javascript 中搜索对象数组中的重复字符串

python - 检测几乎重复的行

c - WHILE 循环显示与 FOR 循环不同的输出

c - 使程序读取文件的某些部分