C Program to Print Unique Elements of An Unsorted Array

Here is the C program to print unique elements of an array. Given an unsorted array of length N, we have to print the unique elements of array. If there are three occurrences of 5 then we have to print 5 only once.

We can find the unique element in an array by traversing the array from index 0 to N-1 and for each element again traversing the array to find any duplicated element.

For Example

Input Array: 8 3 6 1 7 3 7 8
Unique elements : 8 3 6 1 7

Algorithm to find unique elements of unsorted array
  • First of all, take N numbers as input from user and store it in an array(lets call it inputArray).
  • We will start traversing inputArray from index 0 to N -1 and for any element at index i(inputArray[i]), we will search for duplicate element from index 0 to i.
  • If we find a duplicate element then we skip current element otherwise print it on screen.
Time Complexity : O(n2)

C program to find unique elements of an unsorted array

#include<stdio.h>

int main() {
 int array[100], size, i, j;
 
 printf("Enter number of elements in array\n");
 scanf("%d", &size);
 printf("Enter %d numbers\n", size);
 
 for(i = 0; i < size; i++){
  scanf("%d", &array[i]);
 }
 
 printf("Unique Elements\n");
 for(i = 0; i < size; i++) {
  for (j=0; j<i; j++){
      if (array[i] == array[j])
       break;
       }
     
     if (i == j){
      printf("%d ", array[i]);
  }
 }
 
 return 0;
}
Output
Enter number of elements in array
10
Enter 10 numbers
1 2 8 5 2 3 8 4 1 6
Unique Elements
1 2 8 5 3 4 6

Related Topics
C program to delete duplicate elements from a sorted array
C Program to find maximum elements in an array
C program to find second largest element in array
C program to find sum of array elements using recursion
C Program to find frequency of characters in a string
C Program to find sum of diagonal elements of matrix
C program to find sum of digits of a number using recursion
C Program to sum each row and column of matrix
List of all C Programs