def longestConsecutive(self, array):
table = dict()
#r = -1
#l = -1
max_ = float('-inf')
for i in array :
table[i] = True
for num in array :
if table[num] == False :
continue
else :
count_s = 1
table[num] = False
left = num - 1
while left in table :
count_s += 1
table[left] = False
left = left - 1
# left_most = left + 1
right = num + 1
while right in table :
count_s += 1
table[right] = False
right = right + 1
#right_most = right -
if count_s > max_ :
max_ = count_s
return max_
Using python hash table , O(N) SPACE AND O(N) TIME
rajatred
#1