Bit Manipulation Trick for Single Number Problem

🔥 Day 142/360 – Learning Something New Today 🚀 Most people miss this simple trick… But once you understand it, problems become much easier 👇 📌 Topic: Bit Manipulation 🧩 Problem: Single Number II 📝 Problem Statement: Find the element that appears once when all others appear three times 🔍 Example: Input: [2, 2, 3, 2] Output: 3 💡 Approach: Bit Manipulation (Bit Counting) ✔ Step 1 – Traverse all 32 bit positions ✔ Step 2 – Count how many numbers have that bit set ✔ Step 3 – If count % 3 == 1, set that bit in answer ⚡ Key Idea: Duplicate numbers contribute bits in multiples of 3, so taking modulo 3 isolates the unique number ⏱ Complexity: Time → O(n) Space → O(1) 📚 What I Learned: Bit-level thinking can simplify problems that look complex with normal counting 💬 Question for You: Can you solve this problem using XOR without counting bits? 🤔 #DSA #Java #Coding #ProblemSolving #InterviewPrep #LeetCode #TechJourney #142DaysOfCode

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories