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

WBOY
Release: 2023-09-05 09:05:10
forward
1294 people have browsed it

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

Arrayis 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
Copy after login

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}
Copy after login

Example

is:

Example

#include  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; }
Copy after login

Output

The repeat elements of the array are : 21
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!