DEV Community

Discussion on: Exploring the Two Sum Interview Question in JavaScript

Collapse
 
moopet profile image
Ben Sinclair • Edited

Reducing this to a list of unique numbers would fail on ([1,2,2,5], 4) because though 2 + 2 == 4, it'd never get checked.

I think I'd do this by something like:

  • set half = total / 2
  • if half is in list more than once, return [half, half]
  • else make it a set, sort the list, and then brute force much as before

Could also hack out things like, if the total is positive, don't check two negatives.

Collapse
 
nas5w profile image
Nick Scialli (he/him)

Hmm, I’m not quite following. When is this reduced to a list of unique numbers?

Collapse
 
moopet profile image
Ben Sinclair

Oh, I totally misread your code. I think when I read you were using new Set() you were calling new Set(nums) to reduce the length of the list you had to process. My bad.