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

前端 未结 9 1542
轮回少年
轮回少年 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 17:57

    Reed Copsey's suggestion to use hash sets was a good one (I hadn't worked with the HashSet class before).

    But I then discovered that the IEnumerable class offers an extension method called Distinct that copies the unique values from a source IEnumerable to a target IEnumerable.

    Borrowing the first line of Reed's sample code, here's the new sample VB.NET code:

        Dim numbers As IEnumerable(Of Integer) = GetInputFromUser()
        Dim isUnique As Boolean = (numbers.Distinct.Count = numbers.Count)
    

    The variable isUnique is True if the numbers IEnumerable contains no duplicate values, or False if it contains one or more duplicate values.

提交回复
热议问题