TOM
TOM

Reputation: 881

Remove dulplicate Value from array in vb?

i have a array like

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

How can i count the number of each value and remove the duplicate the value ?

Upvotes: 2

Views: 1315

Answers (3)

Stefano Cucchi
Stefano Cucchi

Reputation: 86

I suggest you create two temporary arrays; one int "arri" (def value 0) and one string "arrs" (def value "") with a dim of your arr

I pseudocode it

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

Upvotes: 1

BunkerMentality
BunkerMentality

Reputation: 1337

Something like this?

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

Upvotes: 4

SlaneR
SlaneR

Reputation: 714

To counting the number of each value:

arr(0).Length

To removing the duplicated element:

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


Usage:

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

Upvotes: 1

Related Questions