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;
}
Post A Comment:
0 comments: