LeetCode problem #454 — 4Sum II (JavaScript)

Solution #1: Brute force

The obvious brute force solution to this problem is to run 4 for loops, one for each of the supplied arrays. By doing this, we can add together every potential combination of values from the 4 arrays to find out the total number of potential combinations.

Solution #2: Map

Much like with previous x-sum solutions, we’ll employ the use of a Map in order to avoid looping through all of the data in an inner loop.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Duncan McArdle

Duncan McArdle


Full-stack software developer from the UK, author of the Aftermath book series, full time tech-nerd.