Results 1 to 3 of 3

Thread: How to find duplicates within an array list of random numbers.

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    13

    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?

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width