FunctionRacerProblemsRace

Closest Pair of Equal Values

EasyHash mapsPython, JavaScript, TypeScript, Java, C++

Among all pairs of positions i < j with nums[i] == nums[j], find the one with the smallest gap j - i and return that gap.

If no value appears twice (including the empty list), return -1.

Example 1

nums [3,1,3,1,3]
returns 2

The two 3s at positions 0 and 2 are 2 apart, as are the two 1s; the 3s at positions 2 and 4 are also 2 apart. The smallest gap is 2.

Example 2

nums [5,2,2,5]
returns 1

The two 2s sit next to each other, gap 1.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Hash maps problems