From 3c79f08194c2907bb26b17bf12a5ec2d3f1a9457 Mon Sep 17 00:00:00 2001 From: ADARSH <98254294+ADARSH-863@users.noreply.github.com> Date: Tue, 31 Oct 2023 23:17:01 +0530 Subject: [PATCH] Find_Odd_Occurance_XOR.cpp This C++ program finds the number with an odd occurrence in an array using the bitwise XOR operator. By XORing all elements in the array, the program effectively cancels out pairs of the same numbers, leaving behind the element with an odd occurrence. This bitwise operation enables a compact and efficient solution for identifying the number that occurs an odd number of times in the array. --- Find_Odd_Occurance_XOR.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Find_Odd_Occurance_XOR.cpp diff --git a/Find_Odd_Occurance_XOR.cpp b/Find_Odd_Occurance_XOR.cpp new file mode 100644 index 0000000..de6a67d --- /dev/null +++ b/Find_Odd_Occurance_XOR.cpp @@ -0,0 +1,19 @@ +#include + +int findOddOccurrence(int arr[], int n) { + int result = 0; + for (int i = 0; i < n; ++i) { + result ^= arr[i]; + } + return result; +} + +int main() { + int arr[] = {4, 3, 2, 4, 1, 2, 1}; + int n = sizeof(arr) / sizeof(arr[0]); + + int oddOccurrence = findOddOccurrence(arr, n); + std::cout << "The number with odd occurrence is: " << oddOccurrence << std::endl; + + return 0; +}