Bit Manipulation Trick for Unique Element in Array

🔥 Day 139/360 – Learning Something Powerful Today 🚀 Most people miss this simple trick… But once you understand it, problems become much easier 👇 📌 Topic: Bit Manipulation 🧩 Problem: Single Number 📝 Problem Statement: Find the element that appears only once in an array where every other element appears twice. 🔍 Example: Input: [4, 1, 2, 1, 2] Output: 4 💡 Approach: XOR (Optimized ⚡) ✔ Step 1 – Initialize XOR = 0 ✔ Step 2 – XOR all elements in the array ✔ Step 3 – Return XOR (unique element remains) ⚡ Key Idea: Same numbers cancel out using XOR (a ^ a = 0), leaving only the unique number. ⏱ Complexity: Time → O(n) Space → O(1) 📚 What I Learned: Bit manipulation can replace extra data structures and make solutions more efficient. #DSA #Java #Coding #ProblemSolving #InterviewPrep #LeetCode #TechJourney #139DaysOfCode

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories