Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 217 · Hashing / Anagram

Contains Duplicate

LeetCode 217 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

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

எளிதுArrayHash TableSorting
LeetCode இல் திற ↗

நான் 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.