Contains Duplicate

Solution 1

This brute-force solution checks for duplicates by comparing every element in the array against every other element using nested loops.

Pseudocode

  1. Start an outer loop that iterates through each element of the array with an index i.
  2. Start an inner loop that iterates from the element right after i (index i + 1) to the end of the array.
  3. In the inner loop, compare the element at index i with the element at the inner loop's current index j.
  4. If the two elements are equal, a duplicate has been found, so immediately return True.
  5. If the loops finish without finding any matching elements, return False.

Code

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        length = len(nums)
        for i in range(length):
            for j in range(i + 1, length):
                if nums[i] == nums[j]:
                    return True
        return False

Complexity Analysis

Tricks

Solution 2

This solution finds duplicates by first sorting the array, which groups identical elements together, and then checking for adjacent matches in a single pass.

Pseudocode

  1. Sort the input array nums.
  2. Iterate through the sorted array from the first element up to the second-to-last element.
  3. In each iteration, compare the current element with the next element (nums[i] and nums[i+1]).
  4. If any adjacent pair of elements is identical, a duplicate exists, so return True.
  5. If the loop finishes without finding any identical adjacent elements, return False.

Code

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        nums.sort()
        length = len(nums)
        for i in range(length - 1):
            if nums[i] == nums[i + 1]:
                return True
        return False

Complexity Analysis

Tricks

Solution 3

This optimal solution uses a hash set to keep track of numbers encountered, allowing for constant-time duplicate checks in a single pass.

Pseudocode

  1. Initialize an empty hash set, seen, to store numbers we have already processed.
  2. Iterate through each number n in the input array.
  3. For each number, check if it already exists in the seen set.
  4. If it exists, we have found a duplicate, so immediately return True.
  5. If it does not exist, add the number to the seen set to mark that we have now seen it.
  6. If the loop completes without finding any duplicates, return False.

Code

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        seen = set()
        for n in nums:
            if n in seen:
                return True
            else:
                seen.add(n)
        return False

Complexity Analysis

Tricks

Solution 4

This concise, one-line solution leverages the unique property of sets by comparing the length of the original list to the length of a set created from it.

Pseudocode

  1. Convert the entire input list nums into a set. This process automatically removes any duplicate elements.
  2. Compare the size of the new set with the size of the original list.
  3. If the set's size is less than the list's size, it means duplicates were present and removed, so return True. Otherwise, return False.

Code

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        return len(set(nums)) < len(nums)

Complexity Analysis

Tricks