Find integers that divides maximum number of elements of the array | Function to print the integers that divide the maximum number of elements from the array ; Initialize two lists to store rank and factors ; Start from 2 till the maximum element in arr ; Initialize a variable to count the number of elements it is a factor of ; Maximum rank in the rank list ; Print all the elements with rank m ; Driver code
function maximumFactor ( arr ) { var n = arr . length ; var rank = [ ] ; var factors = [ ] ; var max = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; for ( var i = 2 ; i <= max ; i ++ ) { var count = 0 ; for ( var j = 0 ; j < n ; j ++ ) { if ( arr [ j ] % i == 0 ) count += 1 ; rank . push ( count ) ; factors . push ( i ) ; } } var m = rank . reduce ( ( a , b ) => Math . max ( a , b ) ) ; for ( var i = 0 ; i < rank . length ; i ++ ) { if ( rank [ i ] == m ) document . write ( factors [ i ] + " " ) ; } } var arr = [ 120 , 15 , 24 , 63 , 18 ] ; maximumFactor ( arr ) ;