1

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
4
  • Could you clarify, please: do you want to eliminate the duplicates or show only those numbers which are duplicates? Commented Jan 4, 2015 at 21:19
  • I want to show only the numbers that are duplicates Commented Jan 4, 2015 at 21:47
  • The answer I suggested should give you a way to do that. If it is outside of what you're comfortable with using, let us know. Commented Jan 4, 2015 at 21:59
  • Yeah I'm not comfortable using that. That code is beyond my knowledge. Is there a simpler way of doing that? Commented Jan 4, 2015 at 22:59

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

I haven't learned that type of code yet, so I'm pretty sure that the loop would be simpler. Is there a way of doing this without using "HashSet"
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.