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
- Sort the array
- Loop and check if the current item is equal to previous
Solution 2: Use key: value
- Use an object to store key and value.
- Loop and add the current item to the object as key, set its value to 1 or,
- If the object already has that key, mark it by adding 1 to its value.
- 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.