搜索数组中的重复值

时间:2022-02-21 21:22:07

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:

我对C很陌生,我希望能给我一些建议。我正在尝试获取一个数组的7个整数的输入,并搜索它们,看看是否有任何数字只出现一次。这是我到目前为止所得到的:

#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.

我知道这很可怕,但是C对我来说是完全陌生的。我知道如何从用户那里输入7个整数,但是我还不知道从哪里开始索引它们。我也意识到有更先进的方法来解决这个问题;然而,我正在尝试使用一个业余者能够理解的基本概念来寻找解决方案。

4 个解决方案

#1


0  

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

这可以通过O(n ^ 2)算法:

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


3  

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:

搜索副本的最简单方法(虽然不是最有效的方法)是对数组进行排序。您可以使用内置的qsort函数:

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


1  

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

我知道有3种方法可以找到重复的,2种已经被回答过了,所以这是第三种(简化的)-

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

复杂度O(N)时间O(M)内存。

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.

如果数字在某个范围内,比如0 - M, M与N个元素的数量相当,您可以使用一个大小为M+1的数组来检查这个数字是否出现过。

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

注意——不要忘记输入元素应该是正整数,而不是大于M

#4


1  

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];).

首先,包括标准的i/o库,#定义数组大小,声明数组(我调用的是:int条目[size];)。

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

“输入10个数字”后的第一个for循环是主循环,允许您将10个数字推入数组。

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

以下if语句是应用于随后输入的值的测试:

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

1)第一个if语句确保我们在适当的范围内输入值。

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.

2)下面的“else if”表示,如果条目[i] =条目[0](意思是如果这是数组中的第一个对象),我们就不做任何事情,因为没有什么可以与之相比。

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.

3)最后一个“else”包含一个嵌套循环。外部循环初始化为1,因此我们确保在内部循环中进行比较时,我们总是将当前值与前一个值进行比较。

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]);
                }
            }
        }
    }


}

#1


0  

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

这可以通过O(n ^ 2)算法:

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


3  

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:

搜索副本的最简单方法(虽然不是最有效的方法)是对数组进行排序。您可以使用内置的qsort函数:

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


1  

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

我知道有3种方法可以找到重复的,2种已经被回答过了,所以这是第三种(简化的)-

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

复杂度O(N)时间O(M)内存。

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.

如果数字在某个范围内,比如0 - M, M与N个元素的数量相当,您可以使用一个大小为M+1的数组来检查这个数字是否出现过。

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

注意——不要忘记输入元素应该是正整数,而不是大于M

#4


1  

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];).

首先,包括标准的i/o库,#定义数组大小,声明数组(我调用的是:int条目[size];)。

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

“输入10个数字”后的第一个for循环是主循环,允许您将10个数字推入数组。

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

以下if语句是应用于随后输入的值的测试:

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

1)第一个if语句确保我们在适当的范围内输入值。

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.

2)下面的“else if”表示,如果条目[i] =条目[0](意思是如果这是数组中的第一个对象),我们就不做任何事情,因为没有什么可以与之相比。

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.

3)最后一个“else”包含一个嵌套循环。外部循环初始化为1,因此我们确保在内部循环中进行比较时,我们总是将当前值与前一个值进行比较。

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]);
                }
            }
        }
    }


}