✅✅🔥 SIMPLEST PROGRAM | JAVA 🔥 Python 🔥 C++ 🔥 | Simplest Program |  O(n) & O(n)
Approach
A HashMap (indexToNum) for storing index → number.
A HashMap (numToIndices) where each number maps to a TreeSet (sorted list of indices)
Checks if the

# Approach
<!-- Describe your approach to solving the problem. -->
- A `HashMap (indexToNum)` for storing `index → number`.
A `HashMap (numToIndices)` where each number maps to a `TreeSet` (sorted list of indices)
- Checks if the index already has a number:
   - If yes, retrieves the `oldNum`.
   - Removes this `index` from the `TreeSet` associated with the `oldNum`.
   - If `TreeSet` becomes empty, removes the entry from `numToIndices` to free up memory
- Updates `indexToNum`: Puts the new `number` at `index`.
Ensures `numToIndices` has an entry for `number`:
   - If the `number` is not already present, adds an empty `TreeSet<Integer>`.
   - Adds the index to the sorted set for the number.
- Checks if `number` exists in `numbToIndices`:
    - If exists, returns the smallest index using `first()`.
    - If not found, returns `-1`.


# Complexity
- Time complexity: **O(N)**
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: **O(N)**
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# PLEASE UPVOTE IF IT HELPS 
![Upvote Image Leetcode.jpeg](https://assets.leetcode.com/users/images/017db732-775a-40c4-b8e3-df07c8375660_1739010304.059909.jpeg)


# Code
```java []
class NumberContainers {

    private Map<Integer, Integer> indexToNum;
    private Map<Integer, TreeSet<Integer>> numToIndices;

    public NumberContainers() {
        indexToNum = new HashMap<>();
        numToIndices = new HashMap<>();
    }
    
    public void change(int index, int number) {
        if(indexToNum.containsKey(index)){
            int oldNum = indexToNum.get(index);
            numToIndices.get(oldNum).remove(index);

            // check if number has zero indices and remove the number
            if(numToIndices.get(oldNum).isEmpty()){
                numToIndices.remove(oldNum);
            }
        }

        indexToNum.put(index,number);
        numToIndices.putIfAbsent(number, new TreeSet<>());
        numToIndices.get(number).add(index);
    }
    
    public int find(int number) {
        return numToIndices.containsKey(number) ? numToIndices.get(number).first() : -1;
    }
}

```
``` python []
from collections import defaultdict
import bisect

class NumberContainers:

    def __init__(self):
        self.indexToNum = {}
        self.numToIndices = defaultdict(list)

    def change(self, index: int, number: int) -> None:
        if index in self.indexToNum:
            oldNum = self.indexToNum[index]
            self.numToIndices[oldNum].remove(index)
            
            # Remove the key if the list becomes empty
            if not self.numToIndices[oldNum]:
                del self.numToIndices[oldNum]

        self.indexToNum[index] = number
        bisect.insort(self.numToIndices[number], index)

    def find(self, number: int) -> int:
        return self.numToIndices[number][0] if number in self.numToIndices else -1
```
``` C++ []
#include <map>
#include <set>

class NumberContainers {
private:
    std::map<int, int> indexToNum;
    std::map<int, std::set<int>> numToIndices;

public:
    NumberContainers() {}

    void change(int index, int number) {
        if (indexToNum.count(index)) {
            int oldNum = indexToNum[index];
            numToIndices[oldNum].erase(index);

            // Remove the key if the set becomes empty
            if (numToIndices[oldNum].empty()) {
                numToIndices.erase(oldNum);
            }
        }

        indexToNum[index] = number;
        numToIndices[number].insert(index);
    }

    int find(int number) {
        return numToIndices.count(number) ? *numToIndices[number].begin() : -1;
    }
};
```
['Hash Table', 'Python', 'C++', 'Java']