Quick actions

cmd+k|ctrl+k

Navigation

Languages

find_longer_consecutive_streak

Snippet info

Language

Python

Visibility

public

Author

arthurpc02

Created

2024-09-30T22:48:35.952666Z

Updated

2024-09-30T22:48:45.622134Z

# find the longer consecutive streak in an array
# example1 : [6,8,12,7,22,2]
# longer streak is 3 (6,7,8)

# example2: [9,7,6,2,5,1,3,8,4,11,12,13]
# longer streak is 9 (1 until 9)

def find_longer_consecutive_streak(array):
    hashed_array = {}
    highest_streak = 0
    count = 1
    for element in array:
        hashed_array[element] = True
        
    for element in array:
        next_number = element+1
        while next_number in hashed_array:
            count += 1
            next_number += 1
        if count > highest_streak:
            highest_streak = count
            # print(f"highest streak is {highest_streak}, from {element} to {next_number-1}")
        count = 1
        
    return highest_streak


print("Hello World!")
sample1 = [6,8,12,7,22,2]
result1 = find_longer_consecutive_streak(sample1)
print(result1)
assert(result1 == 3)

sample2 = [11,9,7,2,5,1,3,8,4,12,13,6]
result2 = find_longer_consecutive_streak(sample2)
print(result2)
assert(result2 == 9)

print("Goodbye World")
INFO