Question

LeetCode #605

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

Solution 1: Sort the array

  1. Sort the array
  2. Loop and check if the current item is equal to previous

Solution 2: Use key: value

  1. Use an object to store key and value.
  2. Loop and add the current item to the object as key, set its value to 1 or,
  3. If the object already has that key, mark it by adding 1 to its value.
  4. Check if the current key’s value is greater than 1, if so means we’ve seen that key before so it’s a duplicate.

Playground

See the Pen 217. Contains Duplicate by Cherry Wang (@chryw) on CodePen.