Minimum number of operations required to delete all elements of the array | javascript implementation of the above approach ; function to find minimum operations ; sort array ; prepare hash of array ; Driver program
var MAX = 10000 ; var hashTable = Array ( MAX ) . fill ( 0 ) ; function minOperations ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; for ( var i = 0 ; i < n ; i ++ ) hashTable [ arr [ i ] ] ++ ; var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( hashTable [ arr [ i ] ] ) { for ( var j = i ; j < n ; j ++ ) if ( arr [ j ] % arr [ i ] == 0 ) hashTable [ arr [ j ] ] = 0 ; res ++ ; } } return res ; } var arr = [ 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 ] ; var n = arr . length ; document . write ( minOperations ( arr , n ) ) ;