在vb中删除数组中的重复值?

问题描述 投票:1回答:3

我有一个数组,如

arr(0) = {jan,jan,feb,feb,feb,mar,mar,mar,apr,apr}

我如何计算每个值的数量,并删除重复的值?

arrays vb.net duplicate-removal
3个回答
3
投票

就像这样?

Dim grouping = arr.GroupBy(Function(x) x).Select(Function(g) New With {g.Key, Key .Count = g.Count()})

For Each g In grouping
    Console.WriteLine(g.Key & ":" & g.Count)
Next

Dim distinctList = grouping.Select(Function(x) x.Key)

For Each item In distinctList
    Console.WriteLine(item)
Next

1
投票

计算每个值的数量。

arr(0).Length

去掉重复的元素。

Public Sub RemoveDuplications(Of T)(ByRef arr() As T)
    Dim filteredList As List(Of T) = New List(Of T)

    For Each element As T In arr

        ' If element is already added in the list, skip the elementi
        If filteredList.Contains(element) Then Continue For

        ' Add element to the list
        filteredList.Add(element)
    Next

    ' Return filtered array
    arr = filteredList.ToArray()
End Sub

用法:"RemoveDupl"。

RemoveDuplications(Of Integer)(a(0)) 或 `RemoveDuplications(Of Your_Array_Type)(Array_Variable)


1
投票

我建议你创建两个临时数组;一个是 int "arri" (def value 0) 和一个 string "arrs" (def value "") 黯然神伤

我把它伪装成代码

for int iStart = 0 to arr.count
     dim findIT as integer
      for findIT = 0 to arrs.count
          if (arrs(findIT ) = "") then
            arrs(findIT ) = arr(iStart)
            arri(findIT ) = arri(findIT )+1
            exit for
          else if (arrs(findIT ) = arr(iStart)) then
            arri(findIT ) = arri(findIT )+1
            exit for
          end if
      next
next

for int iShow = 0 to arrs.count
    if (Not(arrs(findIT ) = "")) then
        'print arrs(findIT ) has arri(findIT ) occurrences
    else
       exit for
    end if
next
© www.soinside.com 2019 - 2024. All rights reserved.