Back to Browse

Single Number | LeetCode | C++ Explanation

9 views
May 5, 2026
5:43

πŸ”Ή Problem: Single Number πŸ”— LeetCode Problem: https://leetcode.com/problems/single-number/ πŸ“Œ Problem Explanation: Is problem me hume ek integer array nums diya hota hai jisme har element exactly 2 baar aata hai, lekin ek element aisa hota hai jo sirf 1 baar aata hai. Hume us unique (single) element ko find karna hai. πŸ‘‰ Example: nums = [2,2,1] β†’ output = 1 nums = [4,1,2,1,2] β†’ output = 4 Yaha clearly sab elements repeat ho rahe hain except ek, aur wahi answer hai. --- πŸ’‘ Approach (HashMap / Frequency Counting): Is approach me hum counting technique use karte hain: * Ek unordered_map (hashmap) banate hain jo har element ka frequency store karega * Array ko traverse karke har number ka count map me store karte hain * Uske baad map ko iterate karte hain * Jo element ka frequency = 1 hota hai, wahi humara answer hota hai πŸ‘‰ Simple logic: Jo element sirf ek baar aaya hai, wahi return karna hai πŸ‘‰ Why this works: * Problem guarantee karta hai ki sirf ek element unique hoga * Baaki sabka count 2 hoga * Isliye frequency check karke easily answer mil jata hai --- ⏱ Time Complexity: O(n) β€” ek baar array traverse + ek baar map iterate πŸ’Ύ Space Complexity: O(n) β€” hashmap use ho raha hai --- 🧠 Key Concepts: * HashMap (unordered_map) * Frequency counting * Linear traversal --- πŸ“ Notes: * Ye approach beginner-friendly hai * Easy to understand and implement * Interview me pehle ye approach explain karna safe hota hai * Fir optimized approach discuss kiya ja sakta hai --- πŸ”— GitHub Repository: https://github.com/abdulhaque2005 πŸ”— LeetCode Profile: https://leetcode.com/u/pDjnXUuCp8/ πŸ”— LinkedIn: https://www.linkedin.com/in/abdul-haque78/ --- πŸš€ DSA Practice Journey: * 160+ LeetCode Questions * 40 YouTube Videos * C++ + STL based solutions --- #DSA #LeetCode #CPP #Coding #HashMap #Programming #PlacementPrep

Download

0 formats

No download links available.

Single Number | LeetCode | C++ Explanation | NatokHD