C program to count distinct element in an array

Count distinct element in an array

In this section, we will learn, how to print the distinct number in an array in C language.

Given an integer array, we have to print all the distinct element of the input array. input array may contain duplicate elements, we have to print count of distinct number in an array.

Example

input: a[]={10,20,10,9,6,2,6,20}

Output:  5
they are  10,20,9,6,2

C program to count distinct element in an array
C Program to Counting distinct elements in an array

Working

Step 1. Input the size of array from the user.

Step 2. Input the elements of array from the user.

Step 3. Make the array unique. i.e it should contain only distinct element.

Step 4. Print the number of distinct element and distinct elements also.

C code

Method 1

#include   
int main()
{
int n, i , j, count=0, unique = 0;;
printf("Enter size of the array : "); //Declare array size
scanf("%d",&n);
int arr[n]; //Initialize the array size
int newarr[n];
printf("Enter elements of the array : "); //Array Declaration
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
for(i=0; i<n; i++)
{
count = 0;
for(j=0;j<=i;j++)
{
if(arr[i]==arr[j])
{
count++; //increment the number of distinct elements
}
}
if(count==1)
{
newarr[unique] = arr[i];
unique++;
}
}
printf("total distinct number of element : %d \n",unique); //Print the number of distinct elements

printf("The elements that are unique are= ");
for(i=0; i<unique; i++)
{
printf("%d ",newarr[i]);
}
return 0;
}
output
Enter size of the array : 5
Enter elements of the array : 12
11
31
21
11
total distinct number of element : 4
The elementsthat are unique are= 12 11 31 21


Enter size of the array : 6
Enter elements of the array : 11
11
12
1
12
14
total distinct number of element : 4
The elementsthat are unique are= 11 12 1 14
Enter size of the array : 8
Enter elements of the array : 
12
11
13
14
14
15
16
11
total distinct number of element : 6 
The elementsthat are unique are= 12 11 13 14 15 16 

Method 2

#include <stdio.h>
int main()
{
    int size;
    printf("Enter the number of elements \n");
    scanf("%d",&size);
    int a[size];    //Declare an array
    printf("Enter the array elements : ");
    for (int i=0;i<size;i++)     //Initialize the array
    scanf("%d",&a[i]);
    for (int i = 0; i < size; i++) {   //Sort the array
        int temp;
        for (int j = i+1; j < size; j++) 
        {     
           if(a[i] > a[j]) {    
               temp = a[i];    
               a[i] = a[j];    
               a[j] = temp;    
           }     
        }     
    }    
    //Print the sorted array
    printf("Elements after sorting....");
    for (int i=0;i<size;i++)
    printf("%d ",a[i]);
    printf("\n");      
    int count = 0;
    for (int i = 0; i < size; i++)  //Traverse the array
    {
      // Moving the index when duplicate is found
      while (i < size - 1 && a[i] == a[i + 1])
      {
         i++;
      }
      count++;    //Increment count to take a note of distinct element
    }
    printf("The number of distinct elements are %d",count);   //Prints the number of distinct elements
    return 0;
}


Output:-
Enter the number of elements
5
Enter the array elements : 11
2
12
13
14
Elements after sorting....2 11 12 13 14
The number of distinct elements are 5