Efficient way to find missing elements in an integer sequence

前端 未结 16 1357
鱼传尺愫
鱼传尺愫 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:31

    Using collections.Counter:

    from collections import Counter
    
    dic = Counter([10, 11, 13, 14, 15, 16, 17, 18, 20])
    print([i for i in range(10, 20) if dic[i] == 0])
    

    Output:

    [12, 19]
    

提交回复
热议问题