Most Common number validation

mbedford99

New member
Joined
Oct 10, 2006
Messages
2
Programming Experience
Beginner
Hello,

What is the easiest way to take a series of numbers from a couple of text boxes and compare number to number and output only the most common occurance? For example, if you have a number that should be 10224 but you get multiple results such as:

textbox1: 10224
textbox2: 10223
textbox3: 10224
textbox4: 20224
textbox5: 21001

Result: 10224

As you can see, this is different then simply averaging. But if the number should actually be 10224 and each text box has a different value, most times when you break down number by number and output the most common, you can still arrive on the actual number. Textbox 1-5, with the first number there were three instances of a 1 and two instances of a 2. There were more ones than twos so we output the one and move on to the next number and so on.

Sorry to sound redundant but want to make sure I get what I am trying to do clear. Hopefully that makes sense. How would I do this in VB.Net? It is probably simply but I am getting hung up on it.

Thanks in advance!!!

Mike
 
you are talking about finding the mode of an array. (statistics 101)
that consists of two functions.
determining if the array has a majority element. (what do you do when there are no repeated values or more than one)
if it does have only one you want to find the value of it.
read this link:
FUN WITH ARRAYS

research statistics, mode

good luck.
 
Last edited:
David, Thank you for the reply. I didn't realize that this would be so complicated. It seems as though the article you posted is exactly what I am trying to do. I don't quite understand the article and the code appears to be some form of c or java, not sure since I only know VB.

Looks like this is giong to be one tough task. I was going to simply take the first number in the index of each text box by using chars and stuff them each into a variable. Then I was simply going to compare the number in each of the variables and output the most common. I could also do string concatenation and have just one variable and output the most common instance of a number within that string variable which is what the article you posted looks like it does.

Thanks again for the information, if you or anybody else has anything you can add that will help me out, I would greatly appreciate it!

Mike
 
Here's a much simpler example.

I have a software that interacts with FreeDB, a music database. Sometimes the data in the database contains artist/title separated by slash /, sometimes by dash -
In order to decide, for each title i retrieve, i strip out all the alphanumeric characters, and then I count what is left
At the end of the counting procedure I update a hash table with the counts, and at the end of all the titles retrieved, I take a look at the hash table and work out what was the most common non-alpha character I encountered

In your example, you could use a hash table to store the entry in the text box as the key, and the number of times you'd seen it, as the value. At the end of the process, scan your entire hash table, looking for the key with the highest value - that was the most common occurrence.

There are ways to alter the process or other ways to skin the cat entirely; you could sort the array of values and look for greatest run length. You could keep additional counters to determine who performed best in the hashtable scenario while you update the table.. As always, its a tradeoff of speed and storage space. Faster solutions are fatter.
 
This section is performed once for each title I retrieve. CountNonWordCharsInEachTrack is a Dictionary object intended to keep a count of the non-word characters in each track:
VB.NET:
//remove all the non-word chars leaving behind a list of separator candidates
[FONT=Courier New]For Each c As Char In Regex.Replace(tr.Title, "[ 0-9a-zA-Z.()\[\]]", "").ToCharArray [/FONT]
[FONT=Courier New]If countNonWordCharsInEachTrack.ContainsKey(c) Then [/FONT]
[FONT=Courier New]countNonWordCharsInEachTrack(c) += 1 [/FONT]
[FONT=Courier New]Else [/FONT]
[FONT=Courier New]countNonWordCharsInEachTrack(c) = 1 [/FONT]
[FONT=Courier New]End If [/FONT]
[FONT=Courier New]Next[/FONT]
Your analogue to this section would be to simply foreach all of your values, like 10224. If 10224 has been seen before, then increment its count, else set a new occurrence counter = to 1


now I loop through my dictionary, looking for the highest counter, and remembering the key having that highest:
VB.NET:
'find most likely artist/title separator
 
[FONT=Courier New]Dim highestCount As Integer = 0 [/FONT]
[FONT=Courier New]Dim likelySeparator As Char = "/"C [/FONT]
[FONT=Courier New]For Each c As Char In countNonWordCharsInEachTrack.Keys [/FONT]
[FONT=Courier New]If countNonWordCharsInEachTrack(c) > highestCount OrElse countNonWordCharsInEachTrack(c) = highestCount _[/FONT]
[FONT=Courier New] AndAlso countNonWordCharsInEachTrack(c) = "\"C Then [/FONT]
[FONT=Courier New]highestCount = countNonWordCharsInEachTrack(c) [/FONT]
[FONT=Courier New]likelySeparator = c [/FONT]
[FONT=Courier New]End If [/FONT]
[FONT=Courier New]Next[/FONT]
 
'if the most appearing separator appears in more than 75% of tracks
 
[FONT=Courier New]If highestCount < cd.NumberOfTracks * 0.75 Then [/FONT]
[FONT=Courier New]ro.IsVarious = False [/FONT]
[FONT=Courier New]Else [/FONT]
[FONT=Courier New]ro.IsVarious = True [/FONT]
[FONT=Courier New]ro.ArtistTitleSeparator = likelySeparator [/FONT]
[FONT=Courier New]End If[/FONT]
 
Last edited by a moderator:
mbedford99, the code you was linked to was and is clearly stated VB.NET. All you need there is to copy the Mode function and use it like this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ary() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]New [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2]() {10224, 10223, 10224, 20224, 21001}
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] res [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = Mode(ary) [/SIZE][SIZE=2][COLOR=#008000]'= 10224
[/COLOR][/SIZE]

cjard, I converted the posted C# code to VB.Net, you will restrain yourself from posting C# code at the VB.Net forums. If you don't know VB.Net (which I know you do!) then you don't have to try to answer the question. Incidently, the conversion is usually easy with online converters such as the one at Developer Fusion. Hope the converted code still reflects what it was intended to showcase. Opinion discussion about knowing other programming languages is perhaps best fitted in Debate Club, dont' you think?
 
Back
Top