find the number of duplicate values in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dawn123
    New Member
    • Nov 2009
    • 15

    find the number of duplicate values in array

    I have a 2-d array that the user enters tress species and diameter so it looks something like:
    sm 10
    rm 18
    bf 12
    sm10
    sm10
    rm18
    and so on.. I need to be able to seach though my array and find all the duplicate values and know the number of each dupliate value so sm 10 would be 3 and rm 18 would be 2 and so on.. until the end of the array. I then will take that array and seach for a value in another array.. ( i think i may be able to do this second part) but its the finding dupliate values and number of them I'm havinig trouble with, I dont even know where to start. Thanks alot
  • vb5prgrmr
    Recognized Expert Contributor
    • Oct 2009
    • 305

    #2
    From the sounds of it Dawn123, you will have to attack this with a brute force method of looping from beginning to end comparing strings, which in vb is kind of slow. This example as you can see is for a single dimension array and to modify it for a two dimension array just add another outer loop...
    Code:
    Option Explicit
    
    Dim MyArray() As String
    
    Private Sub Form_Load()
    MyArray = Split("sm10,rm18,bf12,sm10,sm10,rm18", ",")
    End Sub
    
    Private Sub Command1_Click()
    
    Dim StringToCount() As String
    Dim CountOfString() As Integer
    Dim OuterLoopCounter As Integer, InnerLoopCounter As Integer
    Dim UpperBoundsOfArray As Integer, LowerBoundsOfArray As Integer
    Dim UniqueStringCounter As Integer
    Dim MessageString As String
    
    LowerBoundsOfArray = LBound(MyArray)
    UpperBoundsOfArray = UBound(MyArray)
    
    For OuterLoopCounter = LowerBoundsOfArray To UpperBoundsOfArray
      
      If IsStringInArray(MyArray(OuterLoopCounter), StringToCount()) = True Then
        For InnerLoopCounter = LBound(StringToCount) To UBound(StringToCount)
          If MyArray(OuterLoopCounter) = StringToCount(InnerLoopCounter) Then
            CountOfString(InnerLoopCounter) = CountOfString(InnerLoopCounter) + 1
            Exit For
          End If
        Next InnerLoopCounter
      Else
        ReDim Preserve StringToCount(UniqueStringCounter) As String
        ReDim Preserve CountOfString(UniqueStringCounter) As Integer
        StringToCount(UniqueStringCounter) = MyArray(OuterLoopCounter)
        CountOfString(OuterLoopCounter) = 1
        UniqueStringCounter = UniqueStringCounter + 1
      End If
      
    Next OuterLoopCounter
    
    For InnerLoopCounter = LBound(StringToCount) To UBound(StringToCount)
      MessageString = MessageString & StringToCount(InnerLoopCounter) & " = " & CountOfString(InnerLoopCounter) & vbNewLine
    Next InnerLoopCounter
    
    MsgBox MessageString
    
    End Sub
    
    Private Function IsStringInArray(StringToTest As String, ArrayToTestAgainst() As String) As Boolean
    
    Dim ForLoopCounter As Integer
    Dim UpperBoundsOfArray As Integer, LowerBoundsOfArray As Integer
    
    'if you really want to know more about this test please read this thread
    'http://www.tek-tips.com/viewthread.cfm?qid=1495917
    If (Not ArrayToTestAgainst) = -1 Then Exit Function
    
    LowerBoundsOfArray = LBound(ArrayToTestAgainst)
    UpperBoundsOfArray = UBound(ArrayToTestAgainst)
    
    For ForLoopCounter = LowerBoundsOfArray To UpperBoundsOfArray
      If ArrayToTestAgainst(ForLoopCounter) = StringToTest Then
        IsStringInArray = True
        Exit For
      End If
    Next ForLoopCounter
    
    End Function


    Good Luck

    Comment

    • vb5prgrmr
      Recognized Expert Contributor
      • Oct 2009
      • 305

      #3
      Okay d123, recieved your pm and I did not split an array, I split a string into an array...



      Good Luck

      Comment

      Working...