0

I am trying to create a function if an array of integers has duplicate values, so I came up with the following implementation, and it works:

const containsDuplicate = (nums) => {
    const hashTable = {}
    
    for (let num in nums) {
        const numValue = nums[num]
        if (numValue in hashTable) return true
        hashTable[numValue] = "a"
    }
    
    return false
};

But, I want to know a semantic way to implement this following ES6 style, I tried the .forEach but, I discovered that I cant break out of it.

  • 1
    `return true` inside the `forEach` call returns the callback function you passed to it, not the outside function. – M0nst3R Jun 27 '22 at 19:53
  • 1
    "*way more elegant and looks like the most semantic implementation in ES6*" - then you're looking for `for (const num of nums)`, not `.forEach()` – Bergi Jun 27 '22 at 19:54
  • I edited my question, would you consider submiting an answer to that? – Davi Cheli Miquelim Jun 27 '22 at 19:58

0 Answers0