Single Number | LeetCode | C++ Explanation
πΉ 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 formatsNo download links available.