Know your containers
std::unordered_mapstd::unordered_map
is your friend in this problem. Whenever you've never previously seen a number, simply use the operator[]
or insert
function to add the number and it'sits index. When using findfind
, it will return an iterator, which is a key-value
pair.
eg:
auto location = m.find(numToFind);
location->first
is your key, and
location->second
is your value
When you return, don't use push_back
You can simply return an initializer list like: {i,j}
.