Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved Single Number Problem in C++ #178

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions LeetCode/Find Single Number/C++/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# APPROACH 1:
# Time Complexity: O(N)
# Space Complexity: O(N) => for unordered_map data structure
This C++ function uses a unordered_map data structure to count the frequency of each element in the input vector 'nums'.
It then iterates through the map to find the element with a frequency of 1,
which corresponds to the single number that appears only once in the vector. The function returns this single number as the result.

# APPROACH 2:
# Time Complexity: O(N)
# Space Complexity: O(1)
This C++ function uses the XOR (^) bitwise operation to find the single number in the 'nums' vector efficiently.
It initializes 'result' to 0 and iterates through the vector, XORing each element with 'result.'
Since XORing the same number twice results in 0 (A ^ A = 0), and XORing any number with 0 results in the number itself (A ^ 0 = A),
this process effectively cancels out all the duplicate numbers. Ultimately, 'result' will hold the value of the single number
that appears only once, and it returns this result as the answer.
35 changes: 35 additions & 0 deletions LeetCode/Find Single Number/C++/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SOLUTION 1:
// Time Complexity: O(N)
// Space Complexity: O(N)
// Not an effiecient approach
class Solution {
public:
int singleNumber(vector<int>& nums) {
unordered_map<int,int>mp;
for(int i=0;i<nums.size();i++){
mp[nums[i]]++;
}
int res;
for(auto i:mp){
if(i.second==1){
res= i.first;
}
}
return res;
}
};
//SOLUTION 2:
//Time Complexity: O(N)
//Space Complexity: O(1)
//Efficient approach
class Solution {
public:
int singleNumber(vector<int>& nums) {
int result=0;
for(int i=0;i<nums.size();i++)
{
result=result^nums[i];
}
return result;
}
};
Binary file added LeetCode/Find Single Number/C++/pic1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added LeetCode/Find Single Number/C++/pic2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added LeetCode/Find Single Number/C++/pic3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading