查找重复次数和次数

时间:2013-08-08 09:05:50

标签: c arrays duplicates

我必须编写一个程序,在数组中找到重复项,并在数组中存在每个重复项的次数。这是我的代码:

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

#define DIM 5

int contains (int v[], int dim, int n);

int main(int argc, char** argv) 
{
    int i, j, k = 0, found;
    int v[DIM];
    int dup[DIM] = {0};

    for (i = 0; i < DIM; i++)
    {
        printf("Insert element in the array: ");
        scanf("%d", &v[i]);
    }

    for (i = 0; i < DIM; i++)
    {
        found = 0;

        if (contains (dup, DIM, v[i]) == 0)
        {
            for (j = 0; j < DIM; j++)
            {
                if (v[i] == v[j] && (i != j))
                {
                    found ++;
                    dup[k] = v[i];
                    k++;
                }
            }
            if (found != 0)
            {
                printf("Element %d has been repeated %d times\n", v[i], found);
            }
        }
    }


    return (0);
}


int contains (int v[], int dim, int n)
{
    int i;
    int found = 0;

    for (i = 0; i < dim; i++)
    {
        if (v[i] == n)
        {
            found = 1;
        }
    }

    return found;
}

该计划运作良好,但我认为效率不高。当然,它有一种让程序高效的方法,不是吗?我的意思是继续使用数组而不是任何其他结构

4 个答案:

答案 0 :(得分:1)

一个好的哈希表在线性时间内解决了这个问题。如果您不想使用数据结构,那么这是一个类似C的伪代码的O(n lg n)解决方案:

// given: an array a of n integers

sort(a, n);

for (int i = 0;;) {
    int value = a[i];
    int count = 0;

    do {
        i++;
        count++;
    } while (i < n && a[i] == value);

    if (count > 1)
        printf("Element %d has been repeated %d times\n", value, count);
}

可以使用qsort进行排序。该算法的其余部分依赖于以下事实:在排序之后,所有重复项将被组合在一起。

答案 1 :(得分:0)

当你填充数组时,你应该对你的数组进行排序(用qsort示例),使用二分搜索找到你的数字,然后从索引中搜索是否有重复数据:

array:
0 1 2 5 7 7 48 48 56 72

您正在通过示例搜索7。使用二分法搜索,您可以找到具有O(log n)复杂度的7。让我们调用它的索引IDX(在这个例子中等于5)

数组:

0 1 2 5 7 7 48 48 56 72 103
          ^
          |
         IDX(=5)

现在你必须像这样循环:

int count = 0;
int idx = 5; // must be finded via a dichotomic search
int toFind = array[idx];

    while (idx >= 0 && array[idx] == toFind)
    {
       ++count;
       --idx;
    }
    idx = 5:
    while (idx < arrayLength)
    {
       ++count;
       ++idx;
    }

只有在您可以对数组进行排序以及是否要避免使用哈希表时,此解决方案才有效。

答案 2 :(得分:0)

如果您知道用户输入的数字范围,这是一个O(n)方法。假设您知道数字将在0-99范围内。

这是伪代码。

//Input the array elements in an array named arr[]

static int freq[101]; //create a static array to initialize all elements to 0

//scan the array arr and increment the counter at index of freq array

for(int i=0;i<(arr.length);i++)
    freq[arr[i]]++;

//Now scan the array named freq and print the frequency of each of the numbers.
for(int i=0;i<101;i++)
    if(freq[i]>0)
         printf("Frequency of %d is:- %d ",i,freq[i]);

我希望这会对你有所帮助。

答案 3 :(得分:-1)

#include<stdio.h>
#include<conio.h>
void main()
{
  int i,j,a[5];

  clrscr();

  printf( "Enter the element in array\n" );
  for( i=0; i<5; i++ ) {
    scanf( "%d", &a[i] );
  }
  for(i=0;i<5;i++) {
    for(j=i+1;j<5;j++) {
      if(a[j]==a[i])
        printf("Repeated value is %d",a[i]);
    }
  }

  getch();
}
相关问题