|
-
Sep 20th, 2000, 11:45 PM
#1
Thread Starter
Frenzied Member
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
-
Sep 21st, 2000, 12:34 AM
#2
Conquistador
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
-
Sep 21st, 2000, 12:36 AM
#3
Conquistador
this only searches the values, you will have to write something else for determining which value occurs most,
-
Sep 21st, 2000, 01:49 AM
#4
Thread Starter
Frenzied Member
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
-
Sep 21st, 2000, 03:57 AM
#5
Conquistador
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|