C Program to Print all Repeated Elements with Frequency in an Array

«
»

This is a C Program to print all the repeated numbers with frequency in an array.

Problem Description

This C Program print all the repeated numbers with frequency in an array.

Problem Solution

Prints the repeated numbers with frequency in a given array as shown in the program below.

Program/Source Code

Here is source code of the C Program to print all the repeated numbers with frequency in an array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Print all the Repeated Numbers with Frequency in an Array
 */
#include <stdio.h>
#include <malloc.h>
 
void duplicate(int array[], int num)
{
    int *count = (int *)calloc(sizeof(int), (num - 2));
    int i;
 
    printf("duplicate elements present in the given array are ");
    for (i = 0; i < num; i++)
    {
        if (count[array[i]] == 1)
            printf(" %d ", array[i]);
        else
            count[array[i]]++;
    }
}
 
int main()
{
    int array[] = {5, 10, 10, 2, 1, 4, 2};
    int array_freq = sizeof(array) / sizeof(array[0]);
    duplicate(array, array_freq);
    getchar();
    return 0;
}
Program Explanation

In this C Program, we have defined the array[] variable elements. Compute the division of size of previous array value by the next array[] variable element value. the duplicate() function is used to find the repeated numbers with frequency in an array.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

In duplicate() function using for loop find the duplicate numbers present in the given array. If else condition statement is used to check that count[] array variable with base index value of array[] variable is equal to 1.

If the condition is true, then execute the statement and print the duplicate elements in the array. Otherwise, if the condition is false, then execute the else statement and increment the count variable value.

Runtime Test Cases
 
$ cc pgm71.c
$ a.out
 duplicate elements present in the given array are  10  2

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Take C Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.