Beat 100% of Solutions: The Most Efficient Way to Find the Largest Outlier
Largest Outlier Problem SolutionApproachKey ObservationIf we assume a number is an outlier, then the remaining numbers should sum to (totalsum - outlier), meani
# Largest Outlier Problem Solution

---

## Approach
### Key Observation
If we assume a number is an outlier, then the remaining numbers should sum to `(totalsum - outlier)`, meaning that:`
(totalsum - outlier)/2
` must be a valid special number present in `nums`.

### Edge Case Handling
A special case arises when:

$$
totalsum = 3 \times outlier
$$

In this case, removing `outlier` still leaves `(totalsum - outlier)/2` in the set, potentially causing an incorrect result. To handle this:
1. Check if `totalsum / 3` exists in `nums` **at least twice** (since it must be distinct from the sum element).
2. If true, set it as a candidate for the outlier.

### Efficient Iteration
- Convert `nums` to a **set** for O(1) lookup.
- Iterate through unique numbers in `nums`:
  - Check if `(totalsum - num)/2` exists in the set.
  - Update the maximum outlier found.

This solution runs in **O(n) time** and beats 100% of solutions in performance.

### Why This Approach is More Efficient
Most other solutions would check for the potential outlier as follows:
```python
if potential_outlier in num_counts:
    if potential_outlier != num or num_counts[num] > 1: 
        largest_outlier = max(largest_outlier, potential_outlier)
```
However, this approach is wasteful because we need to check ```potential_outlier != num``` again every time. Instead, we handle the special case upfront (```totalsum = 3 * outlier```), avoiding redundant checks in the main loop.

---

## Implementation
```python
class Solution:
    def getLargestOutlier(self, nums: List[int]) -> int:
        
        totalsum = sum(nums)
        setnum = set(nums)
        maxoutlier = float('-inf')
        
        if totalsum/3 in setnum and nums.count(totalsum/3) > 1:
            maxoutlier = int(totalsum/3)

        for num in [x for x in setnum if x!=totalsum/3]:
            if (totalsum-num)/2 in setnum:
                #potential outlier
                maxoutlier = max(maxoutlier,num)

        return maxoutlier

['Python3']