Given a sorted list such as:
[1, 2, 2, 3, 3, 4]
My goal is to check if there are any numbers repeated, and if so, shift the element and all the numbers before it by one to the left as such:
[1, 2, 2, 3, 3, 4]
[0, 1, 2, 3, 3, 4]
[-1, 0, 1, 2, 3, 4]
right now this is my approach:
def shifting(data):
i = 0
while i< len(data)-1:
if data[i]==data[i+1]:
j=i
while j>=0:
data[j]-=1
j-=1
i+=1
return data
But this is an O(n^2) algorithm and takes a lot of time to run with very long lists. I want to find a more efficient approach. Any ideas?