Check if the array has an element which is equal to sum of all the remaining elements | Function to check if such element exists or not ; Storing frequency in map ; Stores the sum ; Traverse the array and count the array elements ; Only possible if sum is even ; If half sum is available ; Driver Code
function isExists ( a , n ) { let freq = new Map ( ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { freq . set ( a [ i ] , freq . get ( a [ i ] ) == null ? 0 : freq . get ( a [ i ] ) + 1 ) ; sum += a [ i ] ; } if ( sum % 2 == 0 ) { if ( freq . get ( sum / 2 ) != null ) return true ; } return false ; } let a = [ 5 , 1 , 2 , 2 ] ; let n = a . length ; if ( isExists ( a , n ) ) document . write ( " " ) ; else document . write ( " " ) ;