Print all unique elements in the array with C.
Input array elements: 1, 2, 3, 5, 1, 5, 20, 2, 12, 10
All unique elements in the array are: 3, 20, 12, 10
#include <stdio.h>
int main()
{
int arr[100], freq[100];
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;
}
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 4 5 6 2 5 7 8
Unique elements in the array are : 1 3 4 6 7 8
0 Comments