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.