Home  >  Article  >  Backend Development  >  In C language, what are the array elements that appear multiple times?

In C language, what are the array elements that appear multiple times?

WBOY
WBOYforward
2023-09-05 09:05:101308browse

In C language, what are the array elements that appear multiple times?

Array is a container that contains elements of the same data type, and the length needs to be defined in advance. Elements in an array can appear in any order and any number of times. So, in this program, we will find the elements that appear multiple times in an array.

Problem Description - We have been given an array arr[], we need to find the recurring elements in the array and print them.

Let us take an example to understand better.

Example:

Input: arr[] = {5, 11, 11, 2, 1, 4, 2}
Output: 11 2

Explanation

We have an array arr containing some elements, first we compare the next element in the repeat function. Repeat function is used to find duplicate elements in an array. In the repeating function, we use a loop to find the repeating elements in the given array. We will use if else condition to check the count of array elements, if the array element appears once then the count will be 1 and if it appears multiple times then the count will be incremented accordingly. If the count is greater than 1, the element will be printed on the screen. The Chinese translation of

Algorithm

Input : arr[], n the length of array.
Step 1 : For i -> 0 to n, Follow step 2,
Step 2 : For each element of the array. Do :
   Step 2.1 : For j -> i to n repeat step 2.2 - 2.3.
   Step 2.2 : if (arr[i] == arr[j]) -> print arr[i]
   Step 2.3 : else {// do nothing}

Example

is:

Example

#include <stdio.h>
int main() {
   int arr[] = {21, 87, 212, 109, 41, 21};
   int n=7;
   printf("The repeat elements of the array are : ");
   int *count = (int *)calloc(sizeof(int), (n - 2));
   int i;
   for (i = 0; i < n; i++) {
      if (count[arr[i]] == 1)
         printf(" %d ", arr[i]);
      else
         count[arr[i]]++;
   }
   return 0;
}

Output

The repeat elements of the array are : 21

The above is the detailed content of In C language, what are the array elements that appear multiple times?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete