XOR of Prime Frequencies of Characters in a String | 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 find XOR of prime frequencies ; map is used to store character frequencies ; Traverse the map ; Calculate XOR of all prime frequencies ; Driver code
function SieveOfEratosthenes ( prime , p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( var p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( var i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } function xorOfPrime ( s ) { var prime = Array ( 100005 ) . fill ( true ) ; SieveOfEratosthenes ( prime , 10005 ) ; var i , j ; var m = new Map ( ) ; for ( i = 0 ; i < s . length ; i ++ ) { if ( m . has ( s [ i ] ) ) { m . set ( s [ i ] , m . get ( s [ i ] ) + 1 ) ; } else { m . set ( s [ i ] , 1 ) ; } } var result = 0 ; var flag = 0 ; m . forEach ( ( value , key ) => { if ( prime [ value ] ) { result ^= value ; flag = 1 ; } } ) ; if ( ! flag ) return - 1 ; return result ; } var s = " " ; document . write ( xorOfPrime ( s ) ) ;