Searching for duplicate values in an array

被刻印的时光 ゝ 提交于 2019-12-24 02:32:50

问题


I am very new to C and I am hoping for some pointers. I am attempting to take an input of 7 integers of an array and search through them to see if any number appears only once. Here is what I have so far:

#define size 7
int main(void)
{
int array[size], target, i, prev, count;
//Initialize the array
printf("Please enter %d integers", size);
scanf("%d", &target);
prev = array[0];
count = 1;

for(i = 0; i<size; i++)
{
scanf("%d", &array[i]);
...

I realize it is quite terrible but C is completely strange to me. I figured out how to input the 7 integers from the user but I haven't the first clue as to where to start attempting to index them. I also realized that there are more advanced ways to figure it out; however, I am attempting find the solution using basic concepts that an amateur could understand.


回答1:


This can be done in O(n^2) algorithm:

int yes = 1, i, j;
for (i = 0; i < n; ++i)
{
   for (j = i + 1; j < n; ++j) if (arr[i] == arr[j])
   {
       printf("Found a duplicate of %d\n", arr[i]);
       yes = 0;
       break;
   }
   if (!yes) break;
}
if (yes) printf("No duplicates");



回答2:


The simplest way to search for duplicates (though not the most efficient one) is to sort the array. You can use the built-in qsort function as such:

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

/* ... */

qsort (array, size, sizeof(int), compare);

int seen = 0;
for (int i = 1; i < size; ++i) {
  if (array[i] == array[i - 1]) {
    if (!seen) {
      printf("%d\n", array[i]);
      seen = 1;
    }
  } else {
    seen = 0;
  }
}



回答3:


I know 3 methods to find duplicates, 2 are already answered, so here is the third (simplified)-

Complexity O(N) time, O(M) memory.

If the numbers are within some range like 0 - M and M is comparable to N the number of elements, you can use a array of size M+1 to check if the number has appeared before.

Code -

int exists[M+1]; //set M to appropriate value
memset(exists, 0, sizeof(exists)); //set all 0 
for (i = 0; i < N; i++)
{
  if (exists[array[i]])
  {
    printf("Duplicate found\n");
    break; //or something else
  }
  exists[array[i]] = 1;
}

Note - Don't forget that input elements should be positive integers, not greater than M




回答4:


This one is a little pain in the neck to read, so I explain what I did a little bit here:

First, include the standard i/o library, #define array size to whatever you want, declare you array (I called mine: int entries[SIZE];).

The first for loop after "Enter 10 numbers" is the main one, that allows you to push the 10 numbers into the array.

The following if statements are tests that are applied to value entered right after is typed:

1) The first if statement makes sure we enter the value within the proper range.

2) The following 'else if' suggests that if entries[i] = entries[0] (meaning if this is the first object in the array) let's not do anything because there's nothing to compare it to.

3) The last 'else' contains a nested loop. The outer loop gets initialized to 1 so we make sure that in the comparison that takes place in the inner loop we're always comparing the current value against a previous one.

I hope this helps... cheers :)

*/

#include <stdio.h>

#define SIZE 10

//declarations
int entries[SIZE];

int main(void)
{
    printf("Enter 10 numbers:\n");

    for(int i = 0; i <= SIZE-1; i++)
    {
        printf("[%d]:\n", i);
        scanf("%d", &entries[i]);

        if(entries[i] < 10 || entries[i] > 100) {
            printf("Please enter valid number (between 10 and 100)\n");
            scanf("%d", &entries[i]);
        }
        else if(i == 0) {
            ;
        } else
        {
            for(int j = 1; j <= i; j++)
            {
                *//internal loop goes through all the previous entries (entries[i-1], entries[i-2], etc)*
                for(int k = 0; k < j; k++) {
                    if(entries[j] == entries[k])
                        printf("%d is a duplicate value\n", entries[i]);
                }
            }
        }
    }


}


来源:https://stackoverflow.com/questions/12418872/searching-for-duplicate-values-in-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!