This may be fine if our input is small, but this grows quite fast, as every additional element in the input will require a number of operations equivalent to the input length. The time complexity would be O(n^2). A better methodology is to use a data structure-- specifically, a hash map.
Using a hash map, we can set up a separate tracker, where the keys represent the difference between the goal and a number at an index.
The above is a visual of a hash map data structure.
So if we had an array of numbers [1, 3, 5], and the target/goal was 4, here's the example hash map that we'd generate:
xxxxxxxxxx10
const arr = [1, 3, 5];const goal = 4;​const hash = { 3: 0, // 4 - 1 = 3 at index 0 1: 1, // 4 - 3 = 1 at index 1 -1: 2 // 4 - 5 = -1 at index 2}​console.log(hash);OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

