Given an array with duplicate elements give an algorithm to get the count of distinct elements in the array
Respuestas de entrevistas
Anónimo
19 mar 2012
a good one would be HashSet, add all elements into the HashSet and return HashSet's size
11
Anónimo
14 feb 2011
Whats N, size of the given array? What if there are elements in the array taht are larger than N? A better strategy would be to simply use a hashmap to put the elements and their counts. everytime you see the number you increment the count. this runs in order n space and time.
3
Anónimo
16 dic 2014
Just add all the numbers to a Set and return the sum of all numbers from the Set. Set automatically de-duplicates the numbers.
Anónimo
22 ene 2011
create another array initialized to N with 0 in each slot. Go through the first array, each time you see a number, add that number to the respective index in the second array. Then after, just count the number of elements that aren't 0. This runs in O(n).