Single Number

interview_workbook/leetcode/bit_manip /app/src/interview_workbook/leetcode/bit_manip/single_number.py
View Source

Algorithm Notes

Summary: Single Number — notes not yet curated.
Time: Estimate via loops/recurrences; common classes: O(1), O(log n), O(n), O(n log n), O(n^2)
Space: Count auxiliary structures and recursion depth.
Tip: See the Big-O Guide for how to derive bounds and compare trade-offs.

Big-O Guide

Source

"""
Single Number

TODO: Add problem description
"""

import random

from src.interview_workbook.leetcode._registry import register_problem
from src.interview_workbook.leetcode._types import Category, Difficulty


class Solution:
    def solve(self, nums: list[int]) -> int:
        """Return the element that appears only once in the array where every other element appears twice."""
        result = 0
        for num in nums:
            result ^= num
        return result


def demo() -> str:
    """Run a deterministic demo for Single Number."""
    random.seed(0)
    sol = Solution()
    test_values = [
        [2, 2, 1],
        [4, 1, 2, 1, 2],
        [1],
    ]
    print(f"Test values: {test_values}")
    results = {str(lst): sol.solve(lst) for lst in test_values}
    print(f"Final results: {results}")
    return str(results)


register_problem(
    id=136,
    slug="single_number",
    title="Single Number",
    category=Category.BIT_MANIP,
    difficulty=Difficulty.EASY,
    tags=["array", "bit_manipulation"],
    url="https://leetcode.com/problems/single-number/",
    notes="",
)