DEV Community


Discussion on: Two Sum Solution in JavaScript

abmsourav profile image
Keramot UL Islam Author • Edited
  1. In the second function all happening in one loop, so time complexity is O(n). indexOf is an API method. It's way faster than a loop.
  2. Please run the first example and check the output.
lukaszahradnik profile image
Lukáš Zahradník
  1. It's happening in two loops - one in your function and the second one in the indexOf. It is still O(n^2).
  2. Try out twoSum([2,4,6,5,6], 8) for both of your solution and you will see the inconsistency of outputs.