I need a program that counts the occurrences of elements in a list sorted in non-decreasing order.
Right now, my program looks like:
def list_count(L):
occ = []
M = L.copy()
for i in range(len(L)):
occ.append(M.count(L[i]))
return occ
But the issue I have with the is the O(n^2) runtime. If L is sorted in non-decreasing order, is there a way to implement binary searching to bring the runtime down to O(n) or O(logn) without the use of counter?