Threads
find_non_repeat ( 18748 ) - find_non_repeat ( 18748 ) stack: main(bit_manipulation/find_non_repeating_number.cpp:85)
/**
* @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 /// for assert
#include /// for IO operations
#include /// storing the numbers
/**
* @namespace bit_manipulation
* @brief Bit manipulation algorithms
*/
namespace bit_manipulation {
/**
* @namespace find_non_repeating_integer
* @brief Functions to find the non repeating integer
* in an array of repeating integers. [Single
* Number](https://leetcode.com/problems/single-number/)
*/
namespace find_non_repeating_integer {
/**
* @brief The main function implements find single number
* @param nums vector of integers
* @returns returns the integer that occurs only once
*/
int64_t find_non_repeating_integer(const std::vector& nums) {
// 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_non_repeating_integer
} // namespace bit_manipulation
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
// n = 10,2 return 14
std::vector nums_one{1, 1, 2, 2, 4, 5, 5};
std::vector nums_two{203, 3434, 4545, 3434, 4545};
std::vector nums_three{90, 1, 3, 90, 3};
assert(bit_manipulation::find_non_repeating_integer::
find_non_repeating_integer(nums_one) ==
4); // 4 is non repeating
assert(bit_manipulation::find_non_repeating_integer::
find_non_repeating_integer(nums_two) ==
203); // 203 is non repeating
assert(bit_manipulation::find_non_repeating_integer::
find_non_repeating_integer(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;
}
Variables All
| No. | From | Name | Value |
|---|---|---|---|
| NA | NA | NA | NA |
×
Commands and Shortcuts
| No. | Command | Shortcuts | Description |
|---|---|---|---|
| 1 | GB | Alt + LEFT, Alt + A | Go Backward |
| 2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
| 3 | PPE | Alt + UP, Alt + W | Previous Process End |
| 4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
| 5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
| 6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
| 7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
| 8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
| 9 | TB | LEFT, A | current Thread Backward |
| 10 | TF | RIGHT, D | current Thread Foreward |
| 11 | LU | UP, W | go Line Up of current code block in current thread |
| 12 | LD | DOWN, S | go Line Down of current code block in current thread |
| 13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
| 14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
| 15 | BS | Home | go to code Block Start |
| 16 | BE | End | go to code Block End |
| Project: | algorithms-cpp |
| Update: | 20240212 |
| Commit: | 2dadbf73f |
| Source Code: | bit_manipulation.find_non_repeating_number |
| BuildTool: | cpp17 |
| Compiler: | cpp17 |
| Runtime: | Openjdk17 |
| System: | MySystemD |
| Kernel: | Linux5.10.211 |
| Cpu: | Intel:Corei7-7700K |
| Machine: | AwesomeMachine |