Results 1 to 5 of 5

Thread: finding a value in an array

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    Hi.
    does anybody know if there is a quick way to find which value in an array occurs most frequently ?

    For example if I had a list of first names assigned to
    strName()

    and I wanted to know which name came up most often in the list, how could i do that efficiently?

    I know that if I use a couple of for next loops, i can make a very slow one.

    Any ideas?


    Along these lines, is there a way to use code like InStr to search through an array without a loop?
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    try this:
    Code:
    Dim inxFound() As Integer
    Dim strNames(40) As String
    
    Function SearchArray(SearchString As String)
    Dim foundCount As Integer
    Counter = 0
    foundCount = 0
    inxcount = 5
    ReDim inxFound(inxcount)
    For Each Item In strNames
    If Item = SearchString Then
    inxFound(foundCount) = Counter
    foundCount = foundCount + 1
    End If
    Counter = Counter + 1
    If foundCount = inxcount + 1 Then
    inxcount = inxcount + 5
    ReDim inxFound(inxcount)
    End If
    Next Item
    End Function


  3. #3
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    this only searches the values, you will have to write something else for determining which value occurs most,

  4. #4

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    Hi. Thanks guys. This is what I'm using for now. It isn't terribly slow.

    The idea is to find the ten most frequently occurring directors in a long list of movies. I used a "backup" set of arrays so that I could destroy them in the process.


    For intTimes = 1 To 10
    intTopDirMovies(intTimes) = 0
    For intCount = 0 To intNumRecords
    intNumMovies(intCount) = 0
    For x = 0 To intNumRecords
    If strDirector(intCount) <> "" And strDirector(intCount) = strDirector(x) Then intNumMovies(intCount) = intNumMovies(intCount) + 1
    Next x
    If intNumMovies(intCount) > intTopDirMovies(intTimes) Then
    strTopDirector(intTimes) = strDirector(intCount)
    intTopDirMovies(intTimes) = intNumMovies(intCount)
    End If
    Next intCount
    For x = 0 To intNumRecords
    If strDirector(x) = strTopDirector(intTimes) Then strDirector(x) = ""
    Next x
    Next intTimes
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    you can put your code inside code footers by this:
    [code] to end it: [/code]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width