Sunday 2 April 2017

No of Distinct Elements In An Unsorted Array

QUESTION 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.


TEST CASE 1
 
INPUT

5
3 2 3 780 90

OUTPUT

4

TEST CASE 2

INPUT

10
1000 1001 1002 1003 2006 3005 2009 4006 5000 9000

OUTPUT

10


EXPLANATION :

The code requires you to count the number of unique elements present in an array .

ALGORITHM : 

  • Accept the number of elements in the array ( n )
  • Run a loop to accept the numbers into the array ( scanf("%d",&arr[i]) )
  • Remove the redundant elements by checking each number with the rest of the array elements and replacing all other occourances with -999999990
  • After removing the redundant elements count all elements other than -999999990 
  • Print the result.

SOLUTION : (Please use this as reference only)

CODE