Bloomberg interview question

Implement a function that counts the duplicates in an unordered array

Interview Answers

Anonymous

Mar 3, 2016

one could try a solution without ordering, but that is almost certainly not the fastest one, because you would need an outer loop to go through the array with index i and an inner one to see if there are duplicates of the value at position i. You still have to make sure not to count the duplicates twice. This is easier and faster sort the array (this can be done in O(n log n) time) now go once through the array counting the number of blocks of more than two identical numbers // O(n) this number will be the result // how to count the 2+ size blocks ? r=0 // will accumulate to the final result i=1 // start at left of array + 1 while i1 is the size of the array if ( array[i]== array[i-1] ) // duplicate ! { r++; // we found a block with at least 2 identical numbers // discard further duplicates in this block (the specs are slightly unclear whether these // would count as *additional* duplicates .. I would not think so i++ ; while ( i

Anonymous

Mar 3, 2016

Some high-level software apparently ate part of my previously posted solution while i1 is the size of the array should be while i1 is the size of the array ..

Anonymous

Mar 3, 2016

who programmed this system ???? it happened again while i1 is the size of the array should be while i is smaller than n comment: where n assumed to be larger than 1 is the size of the array ...