Leetcode – Contains Duplicate Solution in Python

Spread the love

Introduction

The Contains Duplicate problem is a common algorithmic challenge that tests one’s ability to work with arrays and data structures. It is a popular question on LeetCode. This comprehensive article dives into the problem, unpacks various solving strategies, and discusses their efficiencies in Python.

Problem Statement

The Contains Duplicate problem (LeetCode #217) is defined as follows:

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Example: Input: nums = [1,2,3,1] Output: true

This problem requires us to determine whether the given array contains any duplicates.

Approach 1: Brute Force

The most straightforward approach is to check each element against every other element. This method is not efficient but can be a starting point.

def containsDuplicate(nums):
    n = len(nums)
    for i in range(n):
        for j in range(i + 1, n):
            if nums[i] == nums[j]:
                return True
    return False

This approach has a time complexity of O(n^2) and a space complexity of O(1).

Approach 2: Sorting

By sorting the array, equal elements will end up next to each other. We can then easily scan through the sorted array and check for any consecutive duplicate elements.

def containsDuplicate(nums):
    nums.sort()
    for i in range(1, len(nums)):
        if nums[i] == nums[i - 1]:
            return True
    return False

This approach has a time complexity of O(n log n) due to the sorting and a space complexity of O(1).

Approach 3: Using a Hash Set

A more efficient approach involves using a hash set. By traversing the array and inserting each element into a set, we can check if an element is already in the set before inserting it. If it is, that means it is a duplicate.

def containsDuplicate(nums):
    seen = set()
    for num in nums:
        if num in seen:
            return True
        seen.add(num)
    return False

This approach has a time complexity of O(n) and a space complexity of O(n).

Approach 4: Leveraging Python’s Built-in Functions

Python’s built-in functions can be highly efficient. We can use the fact that sets only contain unique elements to check if there is any reduction in the size of the array when converted to a set.

def containsDuplicate(nums):
    return len(nums) != len(set(nums))

This one-liner has the same time and space complexity as Approach 3, but is more concise.

Testing the Solutions

To verify the correctness of these solutions, we can call the function with an input array.

# Example
nums = [1, 2, 3, 4, 5, 2]
print(containsDuplicate(nums))  # Output: True

Conclusion

The Contains Duplicate problem is a classic example that illustrates the importance of choosing the right data structure for an algorithmic challenge. While the brute force approach is rarely practical due to its inefficiency, sorting or employing a hash set provides more performant solutions. Moreover, mastering the art of leveraging the built-in functions in Python can lead to concise and efficient code. As always, understanding the underlying logic and trade-offs between different approaches is crucial in algorithmic problem-solving.

Leave a Reply