【问题标题】:How to determine if there are repeat values in an array如何判断数组中是否有重复值
【发布时间】:2016-08-05 18:23:24
【问题描述】:

我要做的是使用 5 个文本框从用户那里获取一个数字输入,然后将这些数字放入一个数组中。

我将如何确定值是否在数组中重复?就像数组最终是: {2,3,2,8,4} 我不确定如何让程序基本上说“有 2 个文本框的值为 2。”

谢谢。

【问题讨论】:

标签: vb.net


【解决方案1】:

这是一个使用 LINQ 的简单示例:

Sub Main()
   Dim myArray As Integer() = {2, 3, 2, 8, 4}

   For Each group In myArray _
      .GroupBy(Function(i) i) _
      .Where(Function(grp) grp.Count > 1)

      Console.WriteLine($"There are {group.Count} textboxes with the value {group.Key}.")
   Next
End Sub

【讨论】:

    【解决方案2】:

    这应该可以解决问题,假设两个数组属于同一类型:

    Dim hasDupes As Boolean
    Dim array1 As Integer() = {3, 5, 7, 11, 13}
    Dim array2 As Integer() = {1, 3, 5, 7, 9, 11, 13}
    
    hasDupes = array1.Intersect(array2).Any()
    

    如果有任何重复项,hasDupes 将评估为 true。

    【讨论】:

    • OP 说的是 1 个数组,而不是两个。
    猜你喜欢
    • 2014-10-10
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 2014-09-02
    • 2014-05-11
    • 1970-01-01
    相关资源
    最近更新 更多