How to use to make sorted list elements unique with minimum increments in Python

1 Answer

0 votes
def make_unique(lst):
    previous = lst[0]
    
    for i in range(1, len(lst)):
        if lst[i] <= previous: 
            lst[i] = previous
            lst[i] += 1
            
        previous = lst[i]



lst = [ 1, 1, 2, 2, 3, 3, 7, 8, 8, 8, 12, 15, 33, 33, 33 ]
          
make_unique(lst)
          
print(lst)
    
  
  
  
'''
run:
  
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 33, 34, 35]
  
'''

 



answered Dec 11, 2021 by avibootz
...