Sum of elements in an array having composite frequency | C # program to find sum of elements in an array having composite frequency ; Function to create Sieve to check primes ; If composite [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to composite ; Function to return the sum of elements in an array having composite frequency ; Map is used to store element frequencies ; To store sum ; Traverse the map using iterators ; Count the number of elements having composite frequencies ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static readonly int N = 10005 ; static void SieveOfEratosthenes ( List < Boolean > composite ) { for ( int i = 0 ; i < N ; i ++ ) { composite . Insert ( i , false ) ; } for ( int p = 2 ; p * p < N ; p ++ ) { if ( ! composite [ p ] ) { for ( int i = p * 2 ; i < N ; i += p ) { composite . Insert ( i , true ) ; } } } } static int sumOfElements ( int [ ] arr , int n ) { List < Boolean > composite = new List < Boolean > ( ) ; for ( int i = 0 ; i < N ; i ++ ) composite . Add ( false ) ; SieveOfEratosthenes ( composite ) ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } int sum = 0 ; foreach ( KeyValuePair < int , int > it in mp ) { if ( composite [ it . Value ] ) { sum += ( it . Key ) ; } } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 1 , 1 , 3 , 3 , 2 , 4 } ; int n = arr . Length ; Console . Write ( sumOfElements ( arr , n ) ) ; } }