Write a program to find the number of distinct elements in a sorted 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
3
3
78
90
Sample Output:
3
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)
{
/* No duplicate element found between index 0 to i */
c++;
}
}
printf("%d",c);
return 0;
}
Should the input be a sorted array or unsorted array
ReplyDeleteSorted
Delete