Fastest way to detect duplicate numbers on array vb.net 2005

前端 未结 9 1538
轮回少年
轮回少年 2021-01-20 17:31

I have this project that let user inputs 5 different numbers from 1 to 50. But I want to validate it before saving to DB that i will be 5 unique numbers. What\'s the best an

9条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-20 18:22

    You can use HashSet(Of T) to check this:

    Dim numbers As IEnumerable(Of Integer) = GetInputFromUser()
    Dim hash As HashSet(Of Integer) = new HashSet(Of Integer)(numbers)
    
    Dim unique As Boolean = hash.Count = numbers.Count()
    

    This will be much more efficient than options requiring a sort + iteration.

提交回复
热议问题