> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2024/01-2024-jan-3/04-find-element-occurring-once-when-all-others-are-present-thrice.md).

# 04. Find element occurring once when all others are present thrice

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/find-element-occuring-once-when-all-other-are-present-thrice/1)

![](https://badgen.net/badge/Level/Medium/yellow)

## My Approach

The idea is to use a bitwise counting algorithm.

* We maintain an array `bits` of size 32 to store the count of set bits at each position for all the numbers in the array.
* Then, we iterate through the array and update the `bits` array by counting the set bits for each number.
* Finally, we go through the `bits` array and construct the result by adding the bits where the count is not a multiple of 3.

## Time and Auxiliary Space Complexity

* **Time Complexity:** `O(N)`, where N is the length of the input array.
* **Auxiliary Space Complexity:** `O(1)` (as we use only a constant 32 size vector).

## Code (C++)

```cpp
class Solution {
  public:
    int singleElement(int arr[], int N) {
        vector<int> bits(32, 0);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < 32; j++) {
                bits[j] += arr[i] & 1;
                arr[i] >>= 1;
            }
        }

        int ans = 0;
        for (int i = 0; i < 32; i++)
            if (bits[i] % 3 != 0)
                ans += 1 << i;

        return ans;
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gl01.gitbook.io/gfg-editorials/2024/01-2024-jan-3/04-find-element-occurring-once-when-all-others-are-present-thrice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
