Problem Description
Write a program to find the number of distinct elements in a 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:
Output consists of a single integer which corresponds to the number of distinct elements in the array.CODING ARENA::
#include <stdio.h>
int main()
{
int n,a[10],i,j,t,s=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]==a[j])
s++;
printf ("%d",n-s);
return 0;
}
Test Case 1
Input (stdin)5 3 2 3 780 90
Expected Output4
Test Case 2
Input (stdin)10 1000 1001 1002 1003 2006 3005 2009 4006 5000 9000
Expected Output10
why swaping?
ReplyDelete