TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
find_non_repeating_number.cpp File Reference

Implementation to find the non repeating integer in an array of repeating integers. Single Number More...

#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for find_non_repeating_number.cpp:

Go to the source code of this file.

Namespaces

namespace  bit_manipulation
 for assert
 
namespace  find_non_repeating_integer
 Functions to find the non repeating integer in an array of repeating integers. Single Number
 

Functions

int64_t bit_manipulation::find_non_repeating_integer::find_non_repeating_integer (const std::vector< int > &nums)
 The main function implements find single number.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation to find the non repeating integer in an array of repeating integers. Single Number

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

Definition in file find_non_repeating_number.cpp.

Function Documentation

◆ find_non_repeating_integer()

int64_t bit_manipulation::find_non_repeating_integer::find_non_repeating_integer ( const std::vector< int > & nums)

The main function implements find single number.

Parameters
numsvector of integers
Returns
returns the integer that occurs only once

Definition at line 39 of file find_non_repeating_number.cpp.

39 {
40 // The idea is based on the property of XOR.
41 // We know that 'a' XOR 'a' is '0' and '0' XOR 'b'
42 // is b.
43 // Using this, if we XOR all the elements of the array,
44 // the repeating elements will give '0' and this '0'
45 // with the single number will give the number itself.
46
47 int _xor = 0;
48
49 for (const int& num: nums) {
50 _xor ^= num;
51 }
52
53 return _xor;
54}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 85 of file find_non_repeating_number.cpp.

85 {
86 test(); // run self-test implementations
87 return 0;
88}
static void test()
Self-test implementations.

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 62 of file find_non_repeating_number.cpp.

62 {
63 // n = 10,2 return 14
64
65 std::vector<int> nums_one{1, 1, 2, 2, 4, 5, 5};
66 std::vector<int> nums_two{203, 3434, 4545, 3434, 4545};
67 std::vector<int> nums_three{90, 1, 3, 90, 3};
68
69 assert(bit_manipulation::find_non_repeating_integer::
71 4); // 4 is non repeating
72 assert(bit_manipulation::find_non_repeating_integer::
74 203); // 203 is non repeating
75 assert(bit_manipulation::find_non_repeating_integer::
76 find_non_repeating_integer(nums_three) ==
77 1); // 1 is non repeating
78
79 std::cout << "All test cases successfully passed!" << std::endl;
80}
Functions to find the non repeating integer in an array of repeating integers. Single Number