从vb中的数组中删除dulplicate值?

时间:2015-07-03 07:30:03

标签: arrays vb.net duplicate-removal

我有一个类似

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

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

3 个答案:

答案 0 :(得分: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 :(得分: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


用法:

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

答案 2 :(得分:1)

我建议yoo创建两个临时数组,一个int“到达”(def值为0),一个字符串为“arrs”(def值为“”),带有你的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