Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Program to Find the Distinct Elements of an Array using C

Display all the Distinct elements of an Array

Toady we will learn how to show distinct element present in an array. Distinct elements means unique elements occurs only once in an array.
For Example:-
Input:{1,2,4,1,6,2},elements enter by the user.
Output: {4,6},only 4 and 6 occurs one in an array.
We will give you solution of this query in C Programming Language.

Program to find distinct elements of an array using C

Working:-

  • In this problem, we are to display the distinct elements, i.e , the elements which occur only once , of an array.
  • First we will initialize an array of desired length.
  • Then some elements of the array are taken as input.
  • Then we apply a logic as shown in the given program to display the elements which occur only once in an array.

C Program:-

#include 
#define MAX_SIZE 50
int main()
{
int a[MAX_SIZE], num[MAX_SIZE];
int s, i, j, c;
printf("Enter length of array: ");
scanf("%d", &s);
printf("Enter elements of the array array: ");
for(i=0; i<s; i++)
{
scanf("%d", &a[i]);
num[i] = -1;
}

for(i=0; i<s; i++)
{
c = 1;
for(j=i+1; j<s; j++)
{
if(a[i] == a[j])
{
c++;
num[j] = 0;
}
}

if(num[i] != 0)
{
num[i] = c;
}
}

printf("\nUnique elements in the array are: ");
for(i=0; i<s; i++)
{
if(num[i] == 1)
{
printf("%d ", a[i]);
}
}

return 0;
}