How can I compare two adjacent
elements in an array without running into index out of range error.
I am trying to do something like this:
def count(n):
counter = 1
for i in range(len(n)):
if n[i] == n[i+1]:
counter += 1
print(counter, n[i])
I can see that the problem is when it access the last element in n it can no longer find index i+1 and that's why it generates this error. but I need to know how to avoid this error while still checking the last element in the array or string( without changing the range to the length of n minus 1.