Contains Duplicate

Problem Summary

(Write in your own words, not copied from LeetCode. This forces comprehension.)

What is Being Asked?

(One sentence on the actual task.)

Key Observations

(Patterns, constraints, or hints in the problem statement.)

Approach Taken

(Step-by-step logic or pseudocode before coding.)

Why This Works

(Explain the core reason the solution is correct.)

Main Concepts Used

(Mark the CS concepts or algorithms used.)

Time & Space Complexity

Code

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        # brute force
        # Time: O(n^2)
        # Space: O(n)
        # for each item, check whether that item doesnt exist in the array
        # for i in range(len(nums)): 
        #     for j in range(i + 1, len(nums)):
        #         if nums[i] == nums[j]:
        #             return True

        # optimized solution - two pass
        # one pass to store all the values O(n)
        # another pass to iterate on each O(n)

        mySet = set() # 1,
        for num in nums:
            if num in mySet:
                return True
            mySet.add(num)
        
        return False