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

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

    Very late to the party, but what about something like this (C#, sorry)?

    byte[] hits = new byte[51];
    byte[] entries = new byte[] { 1, 12, 12, 32, 26, 49 };
    
    foreach (var entry in entries)
    {
        hits[entry]++;
    }
    

    The elements in the hits array are automatically initialized to 0. When the foreach loop is complete, hits will contain the occurrence count for every number in entries. If any is greater than 1, you have dupes.

提交回复
热议问题