Uniquify Identifiers
Brachylog, 8 bytes
{|∧ℕ₁}ᵐ≠
Try it online!
Explanation
{ }ᵐ Map on the Input:
Input = Output…
| …or…
∧ℕ₁ …Output is in [1,+inf)
≠ All elements of the output must be different
(Implicit labeling)
Java 8, 158 144 bytes
a->{int m=0;String r="",c=",",b=r;for(int x:a)b+=x+c;for(int x:a)if(r.contains(x+c)){for(;(r+b).contains(++m+c););r+=m+c;}else r+=x+c;return r;}
.contains(m+c);m++)
to.contains(++m+c);)
to save 1 byte, and simultaneously converted to Java 8 to save 13 more bytes.
Explanations:
Try it here.
a->{ // Method with integer-array parameter and String return-type
int m=0; // Lowest integer
String r="", // Result-String
c=",", // Comma delimiter for result String
b=r;for(int x:a)b+=x+c;
// Input array as String
for(int x:a) // Loop (2) over the integers in the array
if(r.contains(x+c)){ // If the result already contains this integer
for(;(r+b).contains(++m+c););
// Inner (3) as long as either the result-String or array-String contains the lowest integer
// and raise the lowest integer before every iteration by 1
r+=m+c; // Append the result with this lowest not-present integer
}else // Else:
r+=x+c; // Append the result-String with the current integer
// End of loop (2) (implicit / single-line body)
return r; // Return the result-String
} // End of method
Ruby, 63 bytes
->a{r=*0..a.size;r.map{|i|[a[i]]-a[0,i]==[]?a[i]=(r-a)[1]:0};a}
Try it online!
Explanation
->a{ # Anonymous function with one argument
r=*0..a.size; # Numbers from 0 to array size
r.map{|i| # For all numbers in range:
[a[i]] # Get array with just a[i]
-a[0,i] # Remove elements from array that are
# also in a[0..i-1]
==[]? # Check if result is an empty array
a[i]= # If true, set a[i] to:
(r-a) # Remove elements from number range
# that are also in input array
[1] # Get second element (first non-zero)
:0}; # If false, no-op
a} # Return modified array