C program for finding repeating element in an array

Repeating element of an array

In this section, we will learn the Java Program to Find the Elements that have Duplicates.

Given an array, print all element whose frequency is one.

Example

Input: a[]= { 1,2,5,2,6,7,5 }

Output: 2,5

There is two number which has more than one frequency.

so there is two number which is repeated.

Java program for repeating element in an arrray

 Algorithm

Step 1. Input the size of array from the user.

Step 2. Input the elements of array from the user.

Step 3. Count the frequency of each element, if it is more than 2 then sent it to new array

Step 4. Print the elements of new array

C Program

 

#include <stdio.h>  
int main()  
{
    int n, i , j, count=0, unique = 0;;
    printf("enter size : ");
    scanf("%d",&n);
    int arr[n];
    int newarr[n];
    printf("enter elements : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
    }
    for(i=0; i<n; i++)
    {
        count = 0;
       for(j=0;j<=i;j++)
        {
            if(arr[i]==arr[j])
            {
                count++;
            }
        }
        if(count>1)
        {
            newarr[unique] = arr[i];
            unique++;
        }
    }
    printf("The Repeating Element of array are : ");
    for(i=0; i<unique; i++)
    {
        printf("%d ",newarr[i]);
    }
    return 0;
}  
output
Enter size of an array
6
Enter elements in an array
12
2
12
3
9
2
The repeating elements are
2,12