2

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 ?

2
  • 1
    Perhaps posting the desired output & some selfwritten code might help us to determine exactly what you're trying to accomplish. Commented Jul 3, 2015 at 8:07
  • i want the out put like {jan,feb,mar,apr} then the no of occourance of jan,feb,mar,apr, Commented Jul 3, 2015 at 8:10

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.