How to find duplicates within an array list of random numbers.
I'm creating an application that creates a list of 20 ramdomly selected numbers within an array and displaying those numbers in a listbox. I then need to display the duplicate numbers in an adjacent listbox by clicking the find duplicates button. How would I be able to access those 20 numbers in order to find the duplicates?
Re: How to find duplicates within an array list of random numbers.
One way would be to sort the list box then loop through the list 1 by one remembering the last item checked then compare that to the next item if they match they are dupes.
Re: How to find duplicates within an array list of random numbers.
Grouping the array by values and selecting the ones that have a count of more than 1 will do it.
Code:
Dim arr As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 5, 7, 9}
Dim dups = arr.GroupBy(Function(v) v).Where(Function(g) g.Count() > 1).Select(Function(g) g.Key).ToArray()