Possible Duplicate:
Easiest way to find duplicate values in a javascript array

<!-- End of automatically inserted text -->

How do I check if an array has duplicate values?

If some elements in the array are the same, then return true. Otherwise, return false.

['hello','goodbye','hey'] //return false because no duplicates exist
['hello','goodbye','hello'] // return true because duplicates exist

Notice I don't care about finding the duplication, only want Boolean result whether arrays contains duplications.

One nice thing about solutions that use Set is O(1) performance on looking up existing items in a list, rather than having to loop back over it.

One nice thing about solutions that use Some is short-circuiting when the duplicate is found early, so you don't have to continue evaluating the rest of the array when the condition is already met.

One solution that combines both is to incrementally build a set, early terminate if the current element exists in the set, otherwise add it and move on to the next element.

<!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-js -->
const hasDuplicates = (arr) => {
  let set = new Set()
  return arr.some(el => {
    if (set.has(el)) return true
    set.add(el)
  })
}

hasDuplicates(["a","b","b"]) // true
hasDuplicates(["a","b","c"]) // false
<!-- end snippet -->

According to JSBench.me, should preform pretty well for the varried use cases. The set size approach is fastest with no dupes, and checking some + indexOf is fatest with a very early dupe, but this solution performs well in both scenarios, making it a good all-around implementation.