C Example | Find the Unique Elements in an Array

This c example code will show us how to find the unique elements in an array.

#include <stdio.h>
#include <stdlib.h>
int uniqueEle(int array[], int n){
   int i,j;
   int count = 1;
   for(i = 0; i < n; i++){
      for(j = 0; j < n; j++){
         if(array[i] == array[j] && i != j)
         break;
      }
      if(j == n ){
         printf("\nunique elements in an array is [%d] : %d \n",count,array[i]);
         ++count;
      }
   }
   return -1;
}
int main(){
   int n,i;
   printf("\nEnter no: of elements : ");
   scanf("%d",&n);
   int array[n];
   printf("\nenter the array elements : ");
   for(i = 0; i < n; i++){
      scanf("%d",&array[i]);
   }
   uniqueEle(array, n);
   return 0;
}

Run this code, we will get:

Run 1:
Enter no: of elements: 5
enter the array elements :
11
11
15
16
13
unique elements in an array is [1] : 15
unique elements in an array is [2] : 16
unique elements in an array is [3] : 13
Run 2:
Enter no: of elements: 4
enter the array elements : 11
12
11
11
unique elements in an array is [1] : 12