Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 217 · Hashing / Anagram

Contains Duplicate

LeetCode 217 · सोपा · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

A set turns 'have I seen this before?' into O(1).

सोपाArrayHash TableSorting

मी Contains Duplicate (LeetCode 217) असा सोडवला — Hashing / Anagram पॅटर्नमधील एक सोपा प्रॉब्लेम. खाली: intuition, टप्प्याटप्प्याने approach, माझा accepted Python कोड, animated test cases आणि line-by-line debugger.

खालील विवरण इंग्रजीत आहे.

Intuition

A duplicate means some value appears twice. Comparing all pairs works but is quadratic; sorting puts equal values next to each other; a set remembers what you've seen so any repeat is caught the moment it appears.

Approach

  1. 1

    Brute force: compare every pair.

  2. 2

    Sort: equal values become neighbours, so compare each element to the previous one.

  3. 3

    Set: add each number; if it's already present, return True.

मी कसा सोडवला · पहिल्या कल्पनेपासून अंतिम उपायापर्यंत

1
Brute force pairsO(n²) time · O(1) space

Check every pair.

2
Sort + neighboursO(n log n) time · O(1) extra

Duplicates end up adjacent.

3
Hash setAcceptedO(n) time · O(n) space

Return on the first repeat.

Brute force pairsPythonO(n²) time · O(1) space
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        for i in range(len(nums)):
            for j in range(i):
                if nums[i]==nums[j]:
                    return True
        return False
Sort + neighboursPythonO(n log n) time · O(1) extra
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        nums.sort()
        for i in range(1,len(nums)):
            if nums[i]==nums[i-1]:
                return True
        return False
Hash setPythonO(n) time · O(n) space
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        seen = set()
        for num in nums:
            if num in seen:
                return True
            seen.add(num)
        return False
स्टेप-थ्रू लोड होत आहे…

Contains Duplicate — वारंवार विचारले जाणारे प्रश्न

Contains Duplicate मागील intuition काय आहे?

A duplicate means some value appears twice. Comparing all pairs works but is quadratic; sorting puts equal values next to each other; a set remembers what you've seen so any repeat is caught the moment it appears.

Contains Duplicate (LeetCode 217) कसा सोडवावा?

1. Brute force: compare every pair. 2. Sort: equal values become neighbours, so compare each element to the previous one. 3. Set: add each number; if it's already present, return True.

Contains Duplicate ची time आणि space complexity किती आहे?

Time: O(n). Space: O(n). (Accepted approach: Hash set.)

Contains Duplicate मध्ये सामान्य चुका कोणत्या?

Sorting mutates the input — fine here, but worth noting.

Contains Duplicate सोडवण्याचे एकापेक्षा जास्त मार्ग आहेत का?

Brute force pairs (O(n²) time · O(1) space): Check every pair. Sort + neighbours (O(n log n) time · O(1) extra): Duplicates end up adjacent. Hash set (O(n) time · O(n) space): Return on the first repeat.