Count of elements not divisible by any other elements of Array | Function to count the number of elements of array which are not divisible by any other element in the array arr [ ] ; Iterate over the array ; Check if the element is itself or not ; Check for divisibility ; Return the final result ; Given array ; Function Call
function count ( a , n ) { let countElements = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let flag = true ; for ( let j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; if ( a [ i ] % a [ j ] == 0 ) { flag = false ; break ; } } if ( flag == true ) ++ countElements ; } return countElements ; } let arr = [ 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 ] ; let n = arr . length ; document . write ( count ( arr , n ) ) ;