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

前端 未结 9 1544
轮回少年
轮回少年 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:09

    To simplifiy lets say the user inputs 5 different numbers from 0 to 49.

    You can create a Boolean Array IsUsed(49) with 50 elements.

    Then when the user input the value iInputNum=30 you can set IsUsed(30)=TRUE. Next time, when the user input the second value iInputNum=7, you can set IsUsed(7)=TRUE In this way you can check in a very fast way if the number was already inserted.

    if IsUsed(iInputNum) then
       'Skip the Input (put the right code here)
    else
       'Accept the number
       IsUsed(iInputNum)=TRUE
       'save iInputNum in the database
    end if
    

    Do not forget to clear the array after inserting all 5 numbers. Remenber to put the right index in order to handle the number 1-50 (e not 0-49)

提交回复
热议问题