# Lone Integer

{% hint style="info" %}
<https://binarysearch.com/problems/Lone-Integer>
{% endhint %}

{% tabs %}
{% tab title="Question" %}
You are given a list of integers `nums` where each integer occurs exactly three times except for one which occurs once. Return the lone integer.

Bonus: can you do it in `O(1)` space?

**Constraints**

* `n ≤ 10,000` where `n` is length of `nums`

#### Example 1

**Input**

```
nums = [2, 2, 2, 9, 5, 5, 5]
```

{% endtab %}

{% tab title="Answer" %}

Using math. You can sum the would be answer if all numbers were present. This is the sum of the set of nums \* 3, say this is ps.\
Then take the sum of nums, this is what you're given. say this is ss

divide the difference of the two sets by two which will give you the number with only one occurence.

(ps - ss) / 2 = number with only one occurence.

Explained differently

> Add each number once and multiply the sum by 3, we will get thrice the sum of each element of the array. Store it as thrice\_sum. Subtract the sum of the whole array from the thrice\_sum and divide the result by 2. The number we get is the required number (which appears once in the array).

This assumes all positive integers are given in the array.

```python
class Solution:
    def solve(self, nums):
        return (sum(set(nums)) * 3 - sum(nums)) / 2
```

{% endtab %}
{% endtabs %}


---

# 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://beesec.gitbook.io/algorithms/strings/maths/lone-integer.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.
