Write a C program to input elements in array and print all unique elements in array. How to find unique elements in array in C programming. Logic to find unique elements in array in C program.
Example
Input
Input array elements: 1, 2, 3, 5, 1, 5, 20, 2, 12, 10
Output
All unique elements in the array are: 3, 20, 12, 10
Required knowledge
Basic Input Output, For loop, Array
Logic to find unique elements in array
Step by step descriptive logic to find unique elements in array.
- Input size and elements in array. Store it in some variable say
size
andarr
. - Find frequency of each element and store it in an array say
freq
. - Print array elements with frequency 1 which is our required unique elements.
Program to print unique elements in array
/**
* C program to print all unique elements in array
*/
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], freq[MAX_SIZE];
int size, i, j, count;
/* Input size of array and elements in array */
printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
freq[i] = -1;
}
/* Find frequency of each element */
for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
if(arr[i] == arr[j])
{
count++;
freq[j] = 0;
}
}
if(freq[i] != 0)
{
freq[i] = count;
}
}
/* Print all unique elements of array */
printf("\nUnique elements in the array are: ");
for(i=0; i<size; i++)
{
if(freq[i] == 1)
{
printf("%d ", arr[i]);
}
}
return 0;
}
Output
Enter size of array: 10 Enter elements in array: 1 2 3 5 1 5 20 2 12 10 Unique elements in the array are: 3 20 12 10
Happy coding 😉