# Single Number II

## Problem Statement

Given a **non-empty** array of integers, every element appears *three* times except for one, which appears exactly once. Find that single one.

{% hint style="info" %}
Note:

Your algorithm should have a linear runtime complexity.
{% endhint %}

## Testcases

```
Example 1:

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

Example 2:

Input: [0,1,0,1,0,1,99]
Output: 99
```

## Algorithm

### Iterative Mapping Approach

1. Initialize a map to store the value of the element and the number of occurrences of the element in the array as a key-value pair.&#x20;
2. Iterate through the array of elements and store the count of each element in the array.&#x20;
3. Find the element in the array which has `count = 1` and return the value of that element.&#x20;

{% hint style="info" %}
Time Complexity: O(n)
{% endhint %}

## Code

```cpp
int singleNumber(vector<int>& nums) {
    unordered_map<int, int> m;
    // Iterate through the vector to store the count of each element in a map
    for(int i = 0; i < nums.size(); i++) {
        if(m.find(nums[i]) != m.end()) {
            m[nums[i]]++;
        }
        else {
            m[nums[i]] = 1;
        }
    }
    // Find the element which occurs only once
    for(int i = 0; i < nums.size(); i++) {
        if(m[nums[i]] == 1) {
            // Return the value of the element
            return nums[i];
        }
    }
    return -1;
}
```


---

# Agent Instructions: 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://keerthisreeraghu.gitbook.io/june-leetcoding-challenge/week-4/single-number-ii.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.
