Efficient way to find missing elements in an integer sequence

前端 未结 16 1371
鱼传尺愫
鱼传尺愫 2020-12-01 04:32

Suppose we have two items missing in a sequence of consecutive integers and the missing elements lie between the first and last elements. I did write a code that does accomp

16条回答
  •  青春惊慌失措
    2020-12-01 05:13

    First we should sort the list and then we check for each element, except the last one, if the next value is in the list. Be carefull not to have duplicates in the list!

    l.sort()
    
    [l[i]+1 for i in range(len(l)-1) if l[i]+1 not in l]
    

提交回复
热议问题