Check for missing number in sequence

前端 未结 14 1540
太阳男子
太阳男子 2020-12-04 17:58

I have an List which contains 1,2,4,7,9 for example.

I have a range from 0 to 10.

Is there a way to determine what numbers are missin

14条回答
  •  失恋的感觉
    2020-12-04 18:14

    Turn the range you want to check into a HashSet:

    public IEnumerable FindMissing(IEnumerable values)
    {
      HashSet myRange = new HashSet(Enumerable.Range(0,10));
      myRange.ExceptWith(values);
      return myRange;
    }
    

    Will return the values that aren't in values.

提交回复
热议问题