I'm trying to implement a Genetic Algorithm and need this for my crossover function. What I need to figure out is the following. If I have the list
[0, 1, 6, 7, 5, 4, 3, 2, 1]
it has duplicate elements. I want to end up with the list
[0, 8, 6, 7, 5, 4, 3, 2, 1]
The reason I end up with this list is that I look at each element from left to right. And I see at index 1, there's the number '1' which also exists in the list. Therefore I change it to an element not in the list. The smallest element in the list is 0 and largest element is len(list) -1.
In this case the order of the list is important. Therefore, I do not think converting it to a set would be appropriate. I do not want to lose the order of the elements. I am just changing elements that are repeated in the list already.
Another example would be
[0, 1, 2, 7, 3, 4, 3, 2, 1]
Would become
[0, 5, 6, 7, 8, 4, 3, 2, 1]
So what happened here was I saw the number 1 at index 1, and realized that from the range of 0 to 8, I am missing the number 5. So 1 got replaced with 5. Similarly I did that for index 2, which I replaced with 6. Lastly I did this for index 4, which was originally a 3 but got replaced with an 8 because it's a duplicate.
What I was thinking was incrementing the duplicate number, then checking if its a duplicate, and repeating until every element in the list was unique. However, I can't think of a way to do this.