Skip to content

Odd one out!

Your Task

You are given an array where every element appears exactly three times except for one element that appears only once. Your task is to find that single element using bitwise operations in the most efficient way possible.

Example:

cpp
Input: [2, 2, 3, 2, 1, 1, 1]
Output: 3

Explanation: 
- 2 appears three times
- 1 appears three times  
- 3 appears only once

Requirements

  1. Use only bitwise operations - No arithmetic operators like +, -, *, /
  2. O(n) time complexity - Process the array in a single pass
  3. O(1) space complexity - Use only a constant amount of extra space
  4. Handle all test cases - Your solution should work for any valid input

Given an array where every element appears exactly three times except for one element that appears only once, find the single element using bitwise operations in the most efficient way possible.

cpp
// TODO: Implement the function to find the element that appears only once
// when all other elements appear exactly three times
// Use bitwise operations for the most efficient solution

int findSingleElement(int arr, int n) {
    // TODO: Your code here
    // Return the element that appears only once

    return 0; // Placeholder return
}