31
4
Introduction
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.
Requirements
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.
Assumptions
- 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.)
Test Cases
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
Scoring
Just a simple code golf. Lowest byte count by this time next week wins.
4Add test case:
6, 6, 1, 2, 3, 4, 5
→6, 7, 1, 2, 3, 4, 5
– Adám – 2017-05-03T08:16:55.6032@Adám Shouldn't
6, 6, ...
give6, 1, ...
? – xnor – 2017-05-03T08:30:43.520@xnor No, *If the number has previously been encountered, replace it with the lowest positive integer not found anywhere in the set.* – Adám – 2017-05-03T08:31:34.947
5@Adám I think you're right about that, but the wording could be clearer, Does "set" refer to the elements encountered, all elements of the input list, or elements in the list as it now stands? – xnor – 2017-05-03T08:39:09.493
@xnor Yes, could be clearer, but it should have said not already used if that was the case. – Adám – 2017-05-03T08:40:31.770
3@xnor The
6, 6, 4, 4, 2, 2
test case confirms Adám's interpretation: the expected output is6, 1, 4, 3, 2, 5
, and not6, 1, 4, 2, 3, 5
. – Fatalize – 2017-05-03T09:04:24.2202Does 0 count as a positive integer for this challenge? – Luke – 2017-05-03T10:38:16.553
@Luke If it did, it'd be the first new number to show up in the test cases, and it doesn't. – Ørjan Johansen – 2017-05-03T20:12:46.897
Obviously, the test cases don't use it. However, it may still be acceptable if an answer uses 0. – Luke – 2017-05-03T21:18:57.207
@Luke 0 is not usually considered positive in the English language. See e.g. this Math SE question.
– Ørjan Johansen – 2017-05-03T22:03:05.417It's declared under Assumptions that positive integers start from 1, not 0. – Hand-E-Food – 2017-05-03T23:44:37.593
I've clarified "not found anywhere in the set." It is now "not found anywhere in the entire input list or any existing output." – Hand-E-Food – 2017-05-03T23:51:06.857