write a c program to find the duplicate elements of a given array and find the count of duplicate elements.

28 Dec 2022 Balmiki Mandal 0 C Programming

C Program to Find Duplicate Elements in an Array

A C program to find duplicate elements of a given array and count the number of duplicates typically involves iterating over each element in the array, comparing it to all subsequent elements in the array, and counting how many times the element appears. 

int a[  ]={ 0,3,1,0,5,1,2,0,4,5}

The Duplicate element existed in an array

0  -  3 times

1  -  2 times

5  -  2 times

C program that finds the duplicate elements of a given array and prints the count of each duplicate element

C programming

#include<stdio.h>

int main() {
    int arr[10]; // array to store 10 numbers
    int freq[10] = {0}; // array to store frequency of each number
    int i, j, count = 0;
    
    // Input 10 numbers into array
    printf("Enter 10 numbers: ");
    for (i = 0; i < 10; i++) {
        scanf("%d", &arr[i]);
    }
    
    // Count the frequency of each number
    for (i = 0; i < 10; i++) {
        for (j = i + 1; j < 10; j++) {
            if (arr[i] == arr[j]) {
                freq[i]++;
                freq[j]++;
            }
        }
    }
    
    // Print duplicate numbers and their count
    printf("Duplicate numbers and their count:\n");
    for (i = 0; i < 10; i++) {
        if (freq[i] > 0) {
            printf("%d - %d\n", arr[i], freq[i]+1);
            count++;
        }
    }
    
    if (count == 0) {
        printf("No duplicates found\n");
    }
    
    return 0;
}

The output of the C program you provided will be a list of all the duplicate numbers in the array and their corresponding counts. For example, if the input array is:

[1, 2, 3, 4, 2, 7, 8, 8, 3]

Then the output will be:

 

Duplicate numbers and their count:
2 - 2
3 - 2
8 - 2

Further Reading:

write a c program to reverse the elements of a given array.

write a c program to delete an element at desired from an array

write a c program to insert an element at a desired position in an array

write a c program that deletes the duplicate elements of an array.

write a c program to find the duplicate elements of a given array and find the count of duplicate elements.

write a c program to print non-repeated numbers of a given array.

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.