text: Sum of elements in an array having prime frequency | Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to return the sum of elements in an array having prime frequency ; Map is used to store element frequencies ; Traverse the map using iterators ; Count the number of elements having prime frequencies ; Driver code
code: function SieveOfEratosthenes ( prime , p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( let i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } function sumOfElements ( arr , n ) { let prime = new Array ( n + 1 ) ; prime . fill ( true ) SieveOfEratosthenes ( prime , n + 1 ) ; let i , j ; let m = new Map ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( m . has ( arr [ i ] ) ) m . set ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; else m . set ( arr [ i ] , 1 ) ; } let sum = 0 ; for ( let it of m ) { if ( prime [ it [ 1 ] ] ) { sum += ( it [ 0 ] ) ; } } return sum ; } let arr = [ 5 , 4 , 6 , 5 , 4 , 6 ] ; let n = arr . length ; document . write ( sumOfElements ( arr , n ) ) ;