Count distinct elements after adding each element of First Array with Second Array | Function to find Occurrence of each element from 1 to 2 * MAX ; Initialise MAX ; Count vector to store count of each element from 1 to 2 * MAX ; Size of Arr1 and Arr2 ; Find the elements of arr3 [ ] and increase count of element by 1 ; Prlet the result ; Given arrays arr1 [ ] and arr2 [ ] ; Function call
function findCount ( Arr1 , Arr2 ) { let MAX = Math . max ( Math . max ( ... Arr1 ) , Math . max ( ... Arr2 ) ) ; let Count = Array . from ( { length : 2 * MAX + 1 } , ( _ , i ) => 0 ) ; let n = Arr1 . length , m = Arr2 . length ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { let element = Arr1 [ i ] + Arr2 [ j ] ; Count [ element ] ++ ; } } for ( let i = 1 ; i <= 2 * MAX ; i ++ ) { if ( Count [ i ] > 0 ) { document . write ( i + " " + Count [ i ] + " " ) ; } } } let arr1 = [ 1 , 2 ] ; let arr2 = [ 1 , 2 , 1 ] ; findCount ( arr1 , arr2 ) ;