Find the length of largest subarray with 0 sum | Returns length of the maximum length subarray with 0 sum ; Creates an empty hashMap hM ; Initialize sum of elements ; Initialize result ; Traverse through the given array ; Add current element to sum ; Look this sum in hash table ; Else put this sum in hash table ; Driver program
function maxLen ( arr ) { let hM = new Map ( ) ; let sum = 0 ; let max_len = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { sum += arr [ i ] ; if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ; if ( sum == 0 ) max_len = i + 1 ; let prev_i = hM . get ( sum ) ; if ( prev_i != null ) max_len = Math . max ( max_len , i - prev_i ) ; else hM . set ( sum , i ) ; } return max_len ; } let arr = [ 15 , - 2 , 2 , - 8 , 1 , 7 , 10 , 23 ] ; document . write ( " " + maxLen ( arr ) ) ;