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

Single Number

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

2·sum(set(nums)) − sum(nums) = the single value (XOR also works).

எளிதுArrayBit Manipulation
LeetCode இல் திற ↗

நான் Single Number (LeetCode 136) ஐ இப்படித் தீர்த்தேன் — Hashing / Anagram பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Every value appears twice except one. Counting works with extra memory; a neat arithmetic trick uses the set of distinct values: doubling their sum counts every value twice, so subtracting the real sum cancels the pairs and leaves the single one.

Approach

  1. 1

    Count occurrences and return the value seen once.

  2. 2

    Or: 2 × sum of distinct values counts everything twice.

  3. 3

    Subtract the actual sum — only the lonely value survives.

நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை

1
Frequency mapO(n) time · O(n) space

Count and pick the 1.

2
Set-sum trickAcceptedO(n) time · O(n) space

2·sum(set) − sum.

Frequency mapPythonO(n) time · O(n) space
class Solution:
    def singleNumber(self, nums: list[int]) -> int:
        freq_mpp = defaultdict(int)
        for num in nums:
            freq_mpp[num]+=1
        for item in freq_mpp:
            if freq_mpp[item]==1:
                return item
        return -1
"""
2 times sum/duplicate sum of unique nums
2*(a+b+c) - (a+a+b+b+c) => c
"""
Set-sum trickPythonO(n) time · O(n) space
class Solution:
    def singleNumber(self, nums: list[int]) -> int:
        return 2*sum(set(nums))- sum(nums)
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Single Number — அடிக்கடி கேட்கப்படும் கேள்விகள்

Single Number பின்னாலுள்ள intuition என்ன?

Every value appears twice except one. Counting works with extra memory; a neat arithmetic trick uses the set of distinct values: doubling their sum counts every value twice, so subtracting the real sum cancels the pairs and leaves the single one.

Single Number (LeetCode 136) ஐ எப்படித் தீர்ப்பது?

1. Count occurrences and return the value seen once. 2. Or: 2 × sum of distinct values counts everything twice. 3. Subtract the actual sum — only the lonely value survives.

Single Number இன் time மற்றும் space complexity என்ன?

Time: O(n). Space: O(n) for the set. (Accepted approach: Set-sum trick.)

Single Number இல் பொதுவான தவறுகள் என்ன?

XOR of all values gives O(1) extra space: pairs cancel to 0.

Single Number ஐத் தீர்க்க ஒன்றுக்கு மேற்பட்ட வழிகள் உள்ளனவா?

Frequency map (O(n) time · O(n) space): Count and pick the 1. Set-sum trick (O(n) time · O(n) space): 2·sum(set) − sum.