Find the largest three distinct elements in an array | JavaScript code to find largest three elements in an array ; It uses Tuned Quicksort with ; avg . case Time complexity = O ( nLogn ) ; to handle duplicate values ; Driver code
function find3largest ( arr ) { arr . sort ( ( a , b ) => a - b ) ; let check = 0 , count = 1 ; for ( let i = 1 ; i <= arr . length ; i ++ ) { if ( count < 4 ) { if ( check != arr [ arr . length - i ] ) { document . write ( arr [ arr . length - i ] + " " ) ; check = arr [ arr . length - i ] ; count ++ ; } } else break ; } } let arr = [ 12 , 45 , 1 , - 1 , 45 , 54 , 23 , 5 , 0 , - 10 ] ; find3largest ( arr ) ;