PoppinWave
PoppinWave

Reputation: 11

Finding duplicates of an element in an array

I have to create a program where the user enters 10 numbers and they are put in an array. Then when they click the "Find Duplicates" button, it finds any numbers that are duplicates and displays the number only once in a listBox. How do I create a loop so that it finds the duplicates? Here is my code so far:

For count = 1 To 10
    num = numbers(count)

    If num = numbers(1) Then
        lstDuplicates.Items.Add(num)
    ElseIf num = numbers(2) Then
        lstDuplicates.Items.Add(num)
        cumulator = cumulator + 1
    ElseIf num = numbers(3) Then
        lstDuplicates.Items.Add(num)
        cumulator = cumulator + 1
    ElseIf num = numbers(4) Then
        lstDuplicates.Items.Add(num)
        cumulator = cumulator + 1
    ElseIf num = numbers(5) Then
        lstDuplicates.Items.Add(num)
        cumulator = cumulator + 1
    ElseIf num = numbers(6) Then
        lstDuplicates.Items.Add(num)
        cumulator = cumulator + 1
    ElseIf num = numbers(7) Then
        lstDuplicates.Items.Add(num)
        cumulator = cumulator + 1
    ElseIf num = numbers(8) Then
        lstDuplicates.Items.Add(num)
        cumulator = cumulator + 1
    ElseIf num = numbers(9) Then
        lstDuplicates.Items.Add(num)
        cumulator = cumulator + 1
    ElseIf num = numbers(10) Then
        lstDuplicates.Items.Add(num)
        cumulator = cumulator + 1
    End If
Next

Upvotes: 1

Views: 951

Answers (3)

Enigmativity
Enigmativity

Reputation: 117175

This would be my way of doing it:

Dim numbers() As Integer = {1, 3, 5, 7, 2, 1, 5, 4, 4, 8}

Dim dups = numbers _
    .GroupBy(Function(x) x) _
    .SelectMany(Function(xs) xs.Skip(1).Take(1))

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 416169

You can get this down to a one-liner:

numbers = numbers.Distinct().ToArray()

Or:

ListBox1.Items.AddRange(numbers.Distinct())

Since you want the duplicates, rather than the distinct items, and you don't want to use linq, I'd do this:

Dim distinct As New HashSet(Of Integer)()
Dim result As New HashSet(Of Integer)()
For Each num As Integer In numbers
   If distinct.Contains(num) Then
       result.Add(num)
   Else
       distinct.Add(num)
   End If
Next num
lstDuplicates.Items.Clear()
lstDuplicates.Items.AddRange(result.ToArray())

Upvotes: 1

Andrew Morton
Andrew Morton

Reputation: 25066

To get only the numbers which are repeated, you can group by the numbers and then find which groups have more than one element:

Option Infer On
Module Module1

    Sub Main()
        ' create test data
        Dim numbers = {1, 3, 5, 7, 2, 1, 5, 4, 4, 8}

        ' get the duplicates
        Dim dups = numbers.GroupBy(Function(a) a).Where(Function(b) b.Count() > 1).Select(Function(c) c.Key)

        ' display them
        For Each dup In dups
            Console.WriteLine(dup)
        Next

        Console.ReadLine()

    End Sub

End Module

Edit: Without using LINQ, you could use a Dictionary to hold each number and the count of that number:

Module Module1

    Sub Main()
        ' create test data
        Dim numbers() As Integer = {1, 3, 5, 7, 2, 1, 5, 4, 4, 8}

        ' get the duplicates
        Dim dups As New Dictionary(Of Integer, Integer) ' number, count of number
        For i = 0 To numbers.Length - 1
            If dups.ContainsKey(numbers(i)) Then
                dups(numbers(i)) += 1
            Else
                dups.Add(numbers(i), 1)
            End If
        Next

        ' display them
        For Each dup In dups
            If dup.Value > 1 Then
                Console.WriteLine(dup.Key)
            End If
        Next

        Console.ReadLine()

    End Sub

End Module

Upvotes: 1

Related Questions