|
-
Apr 15th, 2003, 04:38 AM
#1
Thread Starter
Addicted Member
array duplicate question - tricky
Hi,
I have a problem with finding the number that has the most duplicates within an array containing several thousand entries...
I can sort through and find dupes no problem, can find Min/Max and Average, but how do I go about finding the number that is duplicated the most within this array?
Small Example:
1234, 3323, 4567, 1234, 3323, 3444, 4444, 3323, 6678
I don't know what the real values will be when placed into the array, but in this example, 3323 is duplicated more times than any other number... I need to extract the number that has the most duplicates when looping through the array... Any ideas?
Does not matter how, or when extraction occurs...
Thanx a million in advance,
-Scott
Error... What Error?!?! I've only typed a single character!
______________________________
VB 6.0 EE, Win 2k Pro on AMD Duron 1Ghz/ 384 MB SDRAM
-
Apr 15th, 2003, 04:49 AM
#2
Addicted Member
I guess you could try something like this. Have another array which will contain the count for each number. Every time you encounter a new number in the first array that is largest so far, redim the second array to the value of the number from the first array, and put the value '1' into that position in the second array. If the number is smaller than largest so far, then you don't need to redim; the position already exists in the second array so just put the '1' straight in.
Then when you encounter a number for the second or subsequent time in the first array, increment its count in the second one.
Ok, one problem..... if the numbers in the first array are very big (or even if only one number is) then of course the ubound of the second array will be huge too.
-
Apr 15th, 2003, 04:56 AM
#3
Or make a second array 2 dimensional, count how many times the first number occours then write that number and the number of times in the array
array(1,1) = 1234
array(1,2) = 1
array(2,1) = 3233
array(2,2) = 2
Then find the highest number, from the array(x,2) range, and read then array(x,1) value, that is which number occurs most times
-
Apr 15th, 2003, 05:06 AM
#4
Addicted Member
Only prob with that tho' Lightning, is that every time you encounter a number in the first array you have to find it in your second array before you can increment its count, and add it if it's not there yet.
So your way is process intensive, mine uses memory. So he'll have to make a call on which suits best.
-
Apr 15th, 2003, 05:10 AM
#5
You can easily check wether the number you are going to count is already in the array, if don't check and go on to the next number, proberbly uses less memory as a array (6678) + . But it costs more processortime, I agree, he should deside
edit
Sorry misread your post I said nothing, completly nothing.....
-
Apr 15th, 2003, 05:13 AM
#6
Lively Member
I wrote an algorithm last year based on something similar. It uses an UDT as follows:
Code:
Type WordInformation
word as string
count as int
end type
'*****************************************************************
'This proc count the duplicates held in the array and then
'increments the count of the first word that it is comparing.
'So if there is a duplicate, then the count is incremented and
'the element containig the duplicate is replaced with ""
'*****************************************************************
Dim i%, j%, highcount%
highcount = 1
For i = 1 To UBound(Words) - 1
For j = i + 1 To UBound(Words)
If Words(i).word = Words(j).word Then
Words(i).count = Words(i).count + 1
if Words(i).count > highcount then highcount = Words(i).count
Words(j).word = ""
End If
Next j
Next i
i = 2
For j = 2 To UBound(Words)
If Words(j).word <> "" Then
Words(i) = Words(j)
i = i + 1
End If
Next j
ReDim Preserve Words(1 To i - 1)
where highcount is the most frequent word. You may not need thsecond part of the algorithm
You would just need to modify the type statement at the top and I think it should work. ie change word to number and count to numbercount
Hope this helps
Last edited by darthy; Apr 15th, 2003 at 05:18 AM.
-
Apr 15th, 2003, 07:00 AM
#7
PowerPoster
Re: array duplicate question - tricky
Originally posted by killer_cobra
Hi,
I have a problem with finding the number that has the most duplicates within an array containing several thousand entries...
I can sort through and find dupes no problem, can find Min/Max and Average, but how do I go about finding the number that is duplicated the most within this array?
Small Example:
1234, 3323, 4567, 1234, 3323, 3444, 4444, 3323, 6678
I don't know what the real values will be when placed into the array, but in this example, 3323 is duplicated more times than any other number... I need to extract the number that has the most duplicates when looping through the array... Any ideas?
Does not matter how, or when extraction occurs...
Thanx a million in advance,
-Scott
Try this ... it will give you ideas on some creative uses of string functions to solve this sort of problem. Its kinda clunky and thrown together, but should give you the idea. Definitly cuts down on lines of code if your into that 
VB Code:
Private Sub Command1_Click()
Dim a(9) As String
Dim temp1 As Integer, temp2 As Integer
Dim i As Integer
a(1) = 1234
a(2) = 3323
a(3) = 4567
a(4) = 1234
a(5) = 3323
a(6) = 3444
a(7) = 4444
a(8) = 3323
a(9) = 6678
temp1 = 0
For i = 1 To UBound(a)
If UBound(a) - UBound(Split(Replace(Join(a, "-"), a(i) & "-", ""), "-")) > temp1 Then
temp1 = UBound(a) - UBound(Split(Replace(Join(a, "-"), a(i) & "-", ""), "-"))
temp2 = i
End If
Next i
MsgBox a(temp2) & " repeated " & temp1 & " times."
End Sub
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
|