Optimized duplicate ID prevention.

Doesn't iterate over an array, but keeps an object of counts of used IDs. Now
the first duplicate is numbered as the 'second'. This changes the former zero index
to one.