如果我有一个数组设置为:
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/