Home C Language Basic C Programs Unique Elements in Array in C

Unique Elements in Array in C

Write a program in C to print all unique elements in an array.

#include <stdio.h>

void main()

{

   int arr[100];

   int size, i, j, count;

   printf(“Enter size of array: “);

   scanf(“%d”, &size);    /* Input size of array and elements in array */

   printf(“Enter elements in array: \n”);

   for(i=0; i<size; i++)

      scanf(“%d”, &arr[i]);

   printf(“\nUnique elements in the array are: \n”);

   for(i=0;i<size;i++)

   {

      count=0;

      for(j=0;j<i;j++)

      {

         if(arr[i]==arr[j])

         {

            count=1;

            break;

         }

      }

      if(count==0)

         printf(“%d\t”, arr[i]);

   }

}

Output:

Enter the size of array: 6

Enter elements in array:

6              5              4              1              5              4

Unique elements in the array are:

6              5              4              1