Non | C # program to find Non - repeating Primes ; Function to find count of prime ; Find maximum value in the array ; Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to print Non - repeating primes ; Precompute primes using Sieve ; Create HashMap to store frequency of prime numbers ; Traverse through array elements and Count frequencies of all primes ; Traverse through map and print non repeating primes ; Driver code
using System ; using System . Collections ; using System . Linq ; using System . Collections . Generic ; class GFG { static List < bool > findPrimes ( int [ ] arr , int n ) { int max_val = arr . Max ( ) ; List < bool > prime = new List < bool > ( max_val + 1 ) ; for ( int i = 0 ; i < max_val + 1 ; i ++ ) prime . Add ( true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } return prime ; } static void nonRepeatingPrimes ( int [ ] arr , int n ) { List < bool > prime = findPrimes ( arr , n ) ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) if ( mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] ++ ; else mp . Add ( arr [ i ] , 1 ) ; } foreach ( KeyValuePair < int , int > entry in mp ) { if ( entry . Value == 1 ) Console . WriteLine ( entry . Key ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 6 , 7 , 9 , 7 , 23 , 21 , 3 } ; int n = arr . Length ; nonRepeatingPrimes ( arr , n ) ; } }