Finding the maximum inclusive distance between two duplicate numbers in an array
Avoid unnecessary flag variables

The variable duplicate is unnecessary, because you could drive the same meaning from highestSpan &gt; 0. It's an extra hurdle to keep this flag, I suggest to drop it.

Improving the algorithm

I believe this can be solved in \$O(N)\$ time and space, by using a map to record the position of the first occurrences of numbers. If a number was seen already, check the distance and update the max distance if necessary. Otherwise record the current position.