Write a program to find the number of distinct elements in an unsorted array. [Do it without sorting the array]

Input Format:
Input consists of n+1 integers.
The first integer corresponds to n, the number of elements in the array.
The next n integers correspond to the elements in the array. Assume that the maximum value of n is 15.
Output Format:
The output consists of a single integer which corresponds to the number of distinct elements in the array.

Sample Input:
5
3
2
3
780
90
Sample Output:
4


Program Code:
#include<stdio.h> 
int main() 
{
int array[100], size, i, j,c=0;
scanf("%d", &size);
 for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
for(i = 0; i < size; i++) 
{
for (j=0; j<i; j++)
{
if (array[i] == array[j])
break;
}
if (i == j)
{
c++;
}
}
printf("%d",c);
return 0;
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: