Q19. Write a code to print all distinct elements of a given integer array

Distinct Element :- Distinct elements are nothing but the unique (non-duplicate) elements present in the given array.

Algorithm Distinct Elements In An Array

//Algorithm To Print All Distinct Elements Of A Given Integer Array.
START
Step 1 -> Take array as input arr.
Step 2 -> calculate size of array
Step 3 -> for loop i to n:
for loop j to i:
if arr[i] == arr[j]:
break;
if i == j:
print arr[i]
STOP

Code for Distinct Elements In An Array

//C Program Print All Distinct Elements Of A Given Integer Array.
#include <stdio.h>
int main()
{
int arr[] = {5,4,2,5,8,8,7,5,6,7};
int n = sizeof(arr)/sizeof(arr[0]);
for (int i=0; i<n; i++)
{
int j;
for (j=0; j<i; j++)
if (arr[i] == arr[j])
break;
if (i == j)
printf(“%d “,arr[i]);
}
return 0;
}

Output

5,4,2,8,7,6

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number