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

Remove dulplicate Value from array in vb?

我有一个像

这样的数组
arr(0) = {jan,jan,feb,feb,feb,mar,mar,mar,apr,apr}

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

是这样的吗?

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

统计每个值的个数:

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


用法:

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

我建议你创建两个临时数组;一个 int "arri" (def value 0) 和一个 string "arrs" (def value "") 与你的 arr

我伪编码了它

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