-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
add find_single_number #2061
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
Merged
Merged
add find_single_number #2061
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4188df0
add find_single_number
literalEval e90924a
Merge branch 'master' into master
literalEval 7db4fd1
add fix issues
literalEval 779a938
Merge branch 'master' of https://github.com/literalEval/C-Plus-Plus
literalEval 31eb34b
remove .vscode
literalEval f3fdfeb
Merge branch 'master' into master
literalEval 6172f6f
add .vscode
literalEval f7cc2bc
Merge branch 'master' of https://github.com/literalEval/C-Plus-Plus
literalEval d8fb92f
Update .vscode/settings.json
literalEval 2b20fa3
Merge branch 'master' into master
literalEval 2111331
Merge branch 'master' into master
Panquesito7 a23a941
chore(fix): minor issues
Panquesito7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* @file | ||
* @brief Implementation to find the non repeating integer | ||
* in an array of repeating integers. [Single | ||
* Number](https://leetcode.com/problems/single-number/) | ||
* | ||
* @details | ||
* Given an array of integers in which all of the numbers occur exactly | ||
* twice except one integer which occurs only once. Find the non-repeating | ||
* integer. | ||
* | ||
* Worst Case Time Complexity: O(n) | ||
* Space complexity: O(1) | ||
|
||
* @author [Ravidev Pandey](https://github.com/literalEval) | ||
*/ | ||
|
||
#include <cassert> /// for assert | ||
#include <iostream> /// for IO operations | ||
#include <vector> /// storing the numbers | ||
|
||
/** | ||
* @namespace bit_manipulation | ||
* @brief Bit manipulation algorithms | ||
*/ | ||
namespace bit_manipulation { | ||
/** | ||
* @namespace setKthBit | ||
* @brief Functions to find the non repeating integer | ||
* in an array of repeating integers. [Single | ||
* Number](https://leetcode.com/problems/single-number/) | ||
*/ | ||
namespace find_single_number { | ||
/** | ||
* @brief The main function implements find single number | ||
* @param nums vector of integers | ||
* @returns returns the integer that occurs only once | ||
*/ | ||
int find_single_number(std::vector<int>& nums) { | ||
literalEval marked this conversation as resolved.
Show resolved
Hide resolved
literalEval marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// The idea is based on the property of XOR. | ||
// We know that 'a' XOR 'a' is '0' and '0' XOR 'b' | ||
// is b. | ||
// Using this, if we XOR all the elements of the array, | ||
// the repeating elements will give '0' and this '0' | ||
// with the single number will give the number itself. | ||
|
||
int _xor = 0; | ||
|
||
for (const int& num: nums) { | ||
_xor ^= num; | ||
} | ||
|
||
return _xor; | ||
} | ||
} // namespace find_single_number | ||
} // namespace bit_manipulation | ||
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void test() { | ||
// n = 10,2 return 14 | ||
|
||
std::vector<int> nums_one{1, 1, 2, 2, 4, 5, 5}; | ||
std::vector<int> nums_two{203, 3434, 4545, 3434, 4545}; | ||
std::vector<int> nums_three{90, 1, 3, 90, 3}; | ||
|
||
assert(bit_manipulation::find_single_number::find_single_number(nums_one) == 4); // 4 is non repeating | ||
assert(bit_manipulation::find_single_number::find_single_number(nums_two) == 203); // 203 is non repeating | ||
assert(bit_manipulation::find_single_number::find_single_number(nums_three) == 1); // 1 is non repeating | ||
|
||
std::cout << "All test cases successfully passed!" << std::endl; | ||
} | ||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
test(); // run self-test implementations | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.