param { number [ ] } arr param { number } target function SumFourNumbers ( arr , target ) { const result = [ ] ; if ( arr . length < 4 ) { return result ; } arr . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < arr . length - 3 ; i ++ ) { if ( i > 0 && arr [ i ] === arr [ i - 1 ] ) { continue ; } if ( arr [ i ] + arr [ i + 1 ] + arr [ i + 2 ] + arr [ i + 3 ] > target ) { break ; } for ( let j = i + 1 ; j < arr . length - 2 ; j ++ ) { if ( j > i + 1 && arr [ j ] === arr [ j - 1 ] ) { continue ; } let left = j + 1 ; let right = arr . length - 1 ; while ( left < right ) { let sum = arr [ i ] + arr [ j ] + arr [ left ] + arr [ right ] ; if ( sum === target ) { result . push ( [ arr [ i ] , arr [ j ] , arr [ left ] , arr [ right ] ] ) ; } if ( sum <= target ) { while ( arr [ left ] === arr [ ++ left ] ) ; } else { while ( arr [ right ] === arr [ -- right ] ) ; } } } } return result ; } export { SumFourNumbers } ;