By definition, unique identifiers should be unique. Having multiple identifiers that are the same causes one to retrieve unexpected data. But with data arriving concurrently from multiple sources, it can be difficult to ensure uniqueness. Write a function the uniquifies a list of identifiers.
This is possibly the worst puzzle fluff I have written, but you get the idea.
Given a list of zero or more positive integers, apply the following rules to each number from first to last:
- If the number is the first of its kind, keep it.
- If the number has previously been encountered, replace it with the lowest positive integer not found anywhere in the entire input list or any existing output.
For the solution:
- The solution may be a program or a function.
- The input may be a string, an array, passed as arguments, or keyboard input.
- The output may be a string, an array, or printed to the screen.
- All numbers in the output list are distinct.
- The input list is clean. It only contains positive integers.
- A positive integer has the range from 1 to 231-1.
- Less than 256 MB of memory is available for your program's variables. (Basically, no 2,147,483,648-element arrays are permitted.)
Input: empty Output: empty Input: 5 Output: 5 Input: 1, 4, 2, 5, 3, 6 Output: 1, 4, 2, 5, 3, 6 Input: 3, 3, 3, 3, 3, 3 Output: 3, 1, 2, 4, 5, 6 Input: 6, 6, 4, 4, 2, 2 Output: 6, 1, 4, 3, 2, 5 Input: 2147483647, 2, 2147483647, 2 Output: 2147483647, 2, 1, 3
Just a simple code golf. Lowest byte count by this time next week wins.