查找数组中元素的重复项

时间:2015-01-04 20:01:35

标签: arrays vb.net

我必须创建一个程序,用户输入10个数字并将它们放入数组中。然后,当他们点击"查找重复项"按钮,它会找到任何重复的数字,并在listBox中只显示一次该数字。如何创建循环以便找到重复项?到目前为止,这是我的代码:

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

3 个答案:

答案 0 :(得分:1)

你可以把它变成一个单行:

numbers = numbers.Distinct().ToArray()

或者:

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

既然你想要重复项而不是不同的项,并且你不想使用linq,我就这样做:

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 :(得分:1)

要仅获取重复的数字,您可以按数字分组,然后找出哪些组有多个元素:

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

编辑:不使用LINQ,您可以使用字典来保存每个数字和该数字的计数:

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

答案 2 :(得分:0)

这将是我的方式:

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