Count of elements not divisible by any other elements of Array | C # program for the above approach ; Function to count the number of elements of array which are not divisible by any other element of same array ; Length for bool array ; Hash map for storing the element and it 's frequency ; Update the maximum element ; Boolean array of size of the max element + 1 ; Marking the multiples as false ; To store the readonly count ; Traverse bool array ; Check if i is not divisible by any other array elements and appears in the array only once ; Return the readonly Count ; Driver Code ; Given array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int countEle ( int [ ] a , int n ) { int len = 0 ; Dictionary < int , int > hmap = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { len = Math . Max ( len , a [ i ] ) ; if ( hmap . ContainsKey ( a [ i ] ) ) { hmap [ a [ i ] ] ++ ; } else { hmap . Add ( a [ i ] , 1 ) ; } } bool [ ] v = new bool [ len + 1 ] ; for ( int i = 0 ; i <= len ; i ++ ) { v [ i ] = true ; } for ( int i = 0 ; i < n ; i ++ ) { if ( v [ a [ i ] ] == false ) continue ; for ( int j = 2 * a [ i ] ; j <= len ; j += a [ i ] ) { v [ j ] = false ; } } int count = 0 ; for ( int i = 1 ; i <= len ; i ++ ) { if ( v [ i ] == true && hmap . ContainsKey ( i ) && hmap [ i ] == 1 && hmap [ i ] == 1 ) { count += 1 ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 } ; int n = arr . Length ; Console . Write ( countEle ( arr , n ) ) ; } }