C语言 计算数组中重复元素的数量 - C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44756630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:41:28  来源:igfitidea点击:

Count the number of duplicate elements in an array - C

cduplicates

提问by Mohamed Sharbudeen

Accept an Array of size N and print the total number of duplicate elements (The elements which occur two or more times).

接受一个大小为 N 的数组并打印重复元素的总数(出现两次或多次的元素)。

Input Format: The first line contains N. The second line contains the N positive integer values, each separated by a space.

输入格式:第一行包含 N。第二行包含 N 个正整数值,每个值用空格分隔。

Output Format: The first line contains the count of duplicate elements.(count of elements which has duplicates)

输出格式:第一行包含重复元素的计数。(有重复元素的计数)

The program I wrote works for only two same elements and fails to read more than 2 duplicates.

我编写的程序仅适用于两个相同的元素,并且无法读取 2 个以上的重复项。

#include<stdio.h>
#include <stdlib.h>

int main()
{
int arr[1000],i,j,n,count=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
    scanf("%d",&arr[i]);

}

for(i=0;i<n;i++)
{
    for(j=i+1;j<n;j++)
    {


            if(arr[i]==arr[j])
            {
                count=count+1;
                break;
            }

    }
}
printf("%d",count);

}

Input: n=8

输入:n=8

1 2 3 1 2 1 5 6

1 2 3 1 2 1 5 6

Here the program returns 3 instead of 2 because of 3 duplicates(1 1 1). Suggest some ideas to avoid this...

这里程序返回 3 而不是 2,因为有 3 个重复项 (1 1 1)。提出一些想法来避免这种情况......

Output expected is 2. count of elements which has duplicates(1 has two duplicates and 2 has one duplicate). count(1,2)

预期输出为 2. 具有重复项的元素计数(1 个有两个重复项,2 个有一个重复项)。计数(1,2)

回答by vishal

The below java program takes less time and it is very flexible, increase the y array size based on the number in x array.

下面的java程序花费的时间更少,而且非常灵活,根据x数组中的数字增加y数组大小。

public void countDuplicates() {
    int[] x = {1,2,3,4,5,9,1,0,9,1,1,2,4};
    int[] y = new int[10];
    for(int i =0 ; i< x.length ; i++) {
        y[x[i]] = y[x[i]] + 1;
    }
    int c = 0;
    for (int a:y) {
        System.out.println(c+"---> "+a);
        c++;
    }
}

回答by cse

You can use following code for the same. It first sorts the array:

您可以使用以下代码。它首先对数组进行排序:

#include<stdio.h>
#include <stdlib.h>

int main()
{
    int arr[1000],i,j,n,count=0, min;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }

    for(i=0;i<n;i++)
    {
        min = i;
        for(j=i+1;j<n;j++)
        {
            if(arr[min]>arr[j])
            {
                min = j;    
            }
        }
        {
            int temp = arr[min];
            arr[min] = arr[i];
            arr[i] = temp;
        }
    }

    for(i=1;i<n;i++)
    {
        if(arr[i]==arr[i-1])
        {
            count++;
            while(arr[i]==arr[i-1]) i++;
        }
    }
    printf("%d",count);

    return 0;
}

You can find it working here

你可以找到它在这里工作