slight improvement with a short circuit (find vs filter):
find
filter
const twoNumbersEqual = (a, k) => !!a.find(x => a.includes(k-x))
I just realized both of these solutions fail in the case of twoNumbersEqual([5], 10). This can be fixed by passing the index to includes, as such...
twoNumbersEqual([5], 10)
includes
(a, k) => !!a.find((x, i) => a.includes(k-x, i+1))
This should also theoretically be more performant.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
slight improvement with a short circuit (
find
vsfilter
):I just realized both of these solutions fail in the case of
twoNumbersEqual([5], 10)
. This can be fixed by passing the index toincludes
, as such...This should also theoretically be more performant.