Q:

Write a Python program to count the frequency of consecutive duplicate elements in a given list of numbers

0

Write a Python program to count the frequency of consecutive duplicate elements in a given list of numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

def count_dups(nums):
    element = []
    freque = []
    if not nums:
        return element
    running_count = 1
    for i in range(len(nums)-1):
        if nums[i] == nums[i+1]:
            running_count += 1
        else:
            freque.append(running_count)
            element.append(nums[i])
            running_count = 1
    freque.append(running_count)
    element.append(nums[i+1])
    return element,freque


nums = [1,2,2,2,4,4,4,5,5,5,5]
print("Original lists:")
print(nums)

print("\nConsecutive duplicate elements and their frequency:")
print(count_dups(nums))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now