Find Duplicate Elements in Array in C

How to find duplicate elements in an array in the C programming language? To solve this problem we have to check every element with others. Also See:- Count Repeated Elements in Array in C

Example1:- Unsorted array example,
Array = { 50, 20, 10, 40, 20, 10, 10, 60, 30, 70 };
Repeated elements are: 20 10

Example2:- Sorted array example,
Array = { 55, 55, 55, 60, 60, 70, 75, 75, 80, 85 };
Repeated elements are: 55, 60, 75

C Program to Find Duplicate Elements in an Array

The below program is applicable on any array which can be a sorted or an unsorted array. Here we will create a temporary array of similar length, traverse through the original array, and if the repeated element is found then insert it in the temporary array. If the next element is already available in the temporary array then skip it. The procedure to solve this problem,

a) Take an array and its size.
b) Create a new temporary array (assuming for the worst case when there are no duplicate elements).
c) Traverse through the original array.
d) If the current element is available in the temporary array then skip checking for the current element.
e) Else compare the current element and all next elements.
f) If the match is found then insert it into the temporary array, and stop comparing with the next elements.
g) Finally, display total repeated elements.

C program to find duplicate elements in array

#include <stdio.h>
void findDuplicates(int arr[], int n)
{
  // create another array of similar size
  int temp[n];
  int count = 0;
      
  // traverse original array
  for(int i=0; i<n; i++) {
         
    int element = arr[i];
    int flag = 0;
         
    // check current element is already 
    // checked or not
    for(int j=0; j<count; j++) {
      if(temp[j] == element) {
        flag = 1;
        break;
      }
    }
         
    // if already exist then don't check
    if(flag) {
      continue;
    }
         
    // check occurrence of element
    for(int j=i+1; j<n; j++) {
      if(arr[j] == element) {
        temp[count++] = element;
        // found, therefore break
        break;
      }
    }
  }
      
  // display duplicate elements
  printf("Repeated elements are: ");
  for (int i = 0; i < count; i++) {
    printf("%d ",temp[i]);
  }
}

// main function
int main()
{
  // original array
  int arr[] = {50, 20, 10, 40, 20, 10, 10, 60, 30, 70};

  // find array size
  int size = sizeof(arr)/sizeof(arr[0]);
      
  // find duplicates
  findDuplicates(arr, size);
  
  return 0;
}

Output:-

Repeated elements are: 20 10

If the array is sorted then counting repeated elements in an array will be easy compared to the unsorted array. Let us develop another C program that is only applicable for the sorted array. The below program is applicable only for the sorted array in ascending order, not for the unsorted array or sorted in descending order. 

#include <stdio.h>
// function to find duplicates in sorted array
void findDuplicates(int arr[], int n)
{
  // create another array of similar size
  int temp[n];
  int count = 0;
    
  // traverse original array
  // (don't check first element)
  for(int i=1; i<n; i++) {
   
    // current element
    int element = arr[i];

    // if already exist then don't check
    if(element == temp[count]) {
      continue;
    } 
   
    // check occurrence of element
    for (int j = i + 1; j < n; j++) {
      if (arr[j] == element) {
        temp[count++] = element;
        // found, therefore break
        break;
      }
    }
  }

  // display duplicate elements
  printf("Repeated elements are: ");
  for (int i = 0; i < count; i++) {
    printf("%d ",temp[i]);
  }
}
// main function
int main()
{
  // original array
  int arr[] = { 55, 55, 55, 60, 60, 70, 75, 75, 80, 85 };

  // find array size
  int size = sizeof(arr)/sizeof(arr[0]);
    
  // find duplicates
  findDuplicates(arr, size);
  
  return 0;
}

Output:-

Repeated elements are: 55 60 75

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *