C Program to Print Unique Elements in an Array

How to write a C Program to Print Unique Elements in an Array with an example? Before going into this article, Please refer to the Array article to understand the concept of size, index position, etc.

C Program to Print Unique Elements in an Array Example

This program asks the user to enter Array Size and elements. Next, it is going to find out all the Unique elements (non-duplicate elements) present in this using For Loop.

#include <stdio.h>
 
int main()
{
	int arr[10], FreqArr[10], i, j, Count, Size;
	
	printf("\n Please Enter Number of elements in an array  :   ");
	scanf("%d", &Size);
	
	printf("\n Please Enter %d elements of an Array  :  ", Size);
	for (i = 0; i < Size; i++)
	{
    	scanf("%d", &arr[i]);
    	FreqArr[i] = -1;
   	}     
 
	for (i = 0; i < Size; i++)
	{
		Count = 1;
		for(j = i + 1; j < Size; j++)
		{
    		if(arr[i] == arr[j])
    		{
    			Count++;
    			FreqArr[j] = 0;
    		}
    	}
    	if(FreqArr[i] != 0)
    	{
    		FreqArr[i] = Count;
		}
	}

 	printf("\n List of Unique Array Elemnts in this Array are : ");
 	for (i = 0; i < Size; i++)
  	{
  		if(FreqArr[i] == 1)
  		{
  			printf("%d\t", arr[i]);
		}		
  	}	     
 	return 0;
}
C Program to Print Unique Elements in an Array 1

We already explained the analysis part in our previous Programming example. Please refer Program to Count the Frequency of each Element to understand the analysis. And also, refer to the One dimensional article.

Print Unique Elements in an Array Example 2

It is a different version of the above program. Here we are using multiple if statements.

#include <stdio.h>
 
int main()
{
	int arr[10], i, j, k, isUnique, Size;
	
	printf("\n Please Enter Number of elements in an array  :   ");
	scanf("%d", &Size);
	
	printf("\n Please Enter %d elements of an Array  :  ", Size);
	for (i = 0; i < Size; i++)
	{
    	scanf("%d", &arr[i]);
   	}     
 
	for (i = 0; i < Size; i++)
	{
		isUnique = 1;
		for(j = i + 1; j < Size; j++)
		{
    		if(arr[i] == arr[j])
    		{
    			for(k = j; k < Size; k++)
    			{
    				arr[k] = arr[k + 1];
				}
				Size--;
				j--;
				isUnique = 0;
			}
		}
		if(isUnique != 1)
		{
			for(j = i; j < Size - 1; j++)
			{
				arr[j] = arr[j + 1];
			}
			Size--;
			i--;
		}
	}

 	printf("\n List of Unique Array Elements in this Array are : ");
 	for (i = 0; i < Size; i++)
  	{
 		printf("%d\t", arr[i]);
  	}	     
 	return 0;
}
Please Enter Number of elements in an array  :   10

 Please Enter 10 elements of an Array  :  10 20 10 30 10 40 20 90 5 20

 List of Unique Array Elements in this Array are : 30	40	90	5