Tuesday, January 28, 2025

Contains Duplicate

    Profile Pic of Akash AmanAkash Aman

    Updated: January 2025

    Contains Duplicate
    easy

    💡 Intuition

    • Idea is to track every number.
    • If we track each number as we move forward in the array, we can check whether we've already seen that number in our tracking data structure.

    12341123411123411212341123123411234Iteration 1Iteration 2Iteration 3Iteration 4Iteration 5check presence of 1check presence of 2check presence of 3check presence of 4check presence of 1In every step after checking for no we add that to the set

    🚀 Solution

    go
    func containsDuplicate(nums []int) bool {
        var set = map[int]int{};
    
        for _,value := range nums {
            if _, ok := set[value]; !ok  {
                set[value] = 1
            } else {
                return true;
            }
        }
    
        return false;
    }

    ⏳ Time Complexity

    • Since we are taking single loop for the array of length n, the time complexity will be O(n)

    © 2026 Akash Aman | All rights reserved