Open In App

Python – Ways to print longest consecutive list without considering duplicates element

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given, a list of Numbers, the task is to print the longest consecutive (Strictly) list, without considering duplicate elements. If there is more than one answer, print anyone. These type of problem are quite common while working on some web development projects in Django or flask. 

Below are some ways to solve the above task.

Note : If there are multiple answers, print anyone

Input: 
[1, 2, 3, 5, 6, 6, 7, 9, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21]
Output: 
[13, 14, 15, 16]
Explanation : 
Original list = [1, 2, 3, 5, 6, 6, 7, 9, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21] 
Calculated list = [13, 14, 15, 16, 16, ] 
Unique elements = [13, 14, 15, 16, ] 
 

Method 1: Using Iteration 
The basic method that comes to mind while performing this operation is the naive method of printing longest consecutive list. 

Python3




# Python code to print longest consecutive list.
 
# Input list initialization
Input = [12, 13, 14, 17, 18, 23, 24, 25, 25, 26, 27]
 
# Output list initialization
Output = []
temp = []
last = -1
 
# Iteration
for elem in Input:
    if elem - last == 1:
        temp.append(last)
    else:
        temp.append(last)
        Output.append(temp)
        temp = []
    last = elem
 
ans = []
most = 0
 
for elem in Output:
    if len(elem)> most:
        most = len(elem)
        ans = elem
         
# Printing output
print("Initial List is")
print(Input)
print("Longest Consecutive list is :")
print(ans)


Output

Initial List is
[12, 13, 14, 17, 18, 23, 24, 25, 25, 26, 27]
Longest Consecutive list is :
[12, 13, 14]

Method 2: Using groupby and zip 
Using Groupby and zip is the most elegant way to print the longest consecutive list. 

Python3




# Python code to print longest consecutive list.
 
# Importing
from itertools import groupby
 
# List Initialization
Input = [1, 2, 3, 5, 6, 6, 7, 9, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21]
 
# Using zip
z = zip(Input, Input[1:])
 
# Using groupby
lis = [list(y) for i, y in groupby(z, key = lambda x: (x[1] - x[0]) == 1)]
 
# Taking max according to keylength
out = max(lis, key = len)
 
# Output list Initialization
output = []
 
for elem in out:
    output.append(elem[0])
    output.append(elem[1])
 
# Converting to set
output = list(set(output))
 
# Sorting output
output.sort()
 
# Printing answer
print("Initial list is ")
print(Input)
print("Longest Consecutive list is:")
print(output)


Output

Initial list is 
[1, 2, 3, 5, 6, 6, 7, 9, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21]
Longest Consecutive list is:
[13, 14, 15, 16]

Approach#3: Using Sorting and For Loop

We can also use sorting to simplify the solution. We first sort the input list to ensure that all elements in a consecutive sequence are adjacent to each other. We then loop through the sorted list and check if the current element is one more than the previous element. If it is, we add it to the current sequence. If it is not, we compare the length of the current sequence to the length of the longest consecutive sequence found so far and update it if necessary.

Algorithm

1. Sort the input list
2. Initialize max_seq to an empty list and curr_seq to [lst[0]]
3. Loop through the input list starting at index 1:
a. If the current element is one more than the previous element, add it to curr_seq
b. If it is not, compare the length of curr_seq to max_seq and update max_seq if necessary
c. Reset curr_seq to [current element]
4. Compare the length of curr_seq to max_seq and update max_seq if necessary
5. Return max_seq

Python3




def longest_consecutive_list(lst):
    lst.sort()
    max_seq = []
    curr_seq = [lst[0]]
    for i in range(1, len(lst)):
        if lst[i] == lst[i-1] + 1:
            curr_seq.append(lst[i])
        else:
            if len(curr_seq) > len(max_seq):
                max_seq = curr_seq
            curr_seq = [lst[i]]
    if len(curr_seq) > len(max_seq):
        max_seq = curr_seq
    return max_seq
 
lst =[1, 2, 3, 5, 6, 6, 7, 9, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21]
print(longest_consecutive_list(lst))


Output

[13, 14, 15, 16]

Time Complexity: O(nlogn) due to sorting
Space Complexity: O(n) 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads