views:

35

answers:

3

Hello I got this comma separated file with a bunch of numbers

The only thing that I need to be able to do is to find what number that appears the most time

Ex: 817;9;516;11;817;408;9;817

then the result will be 817 I hope you understand what I am trying to do.

A: 

I would suggest using the FileSystemObjects, specifically the OpenTextFile method to read the file, then use split function to separate based on columns. Then iterate the array returned, and count the number of times each number occurs.

The following code will count your array for you. It uses the useful Dictionary object.

Set counts = CreateObject("Scripting.Dictionary")

For i = Lbound(arr) to Ubound(arr)
    If Not counts.Exists(arr(i)) Then
        counts.add arr(i), 1
    Else
        currCount = counts.Item(arr(i))
        counts.Item(arr(i)) = currCount + 1
    End If
Next 

nums = counts.Keys()
currMax = 0
currNum = 0

For i = Lbound(nums) to Ubound(nums)
    If counts.Item(nums(i)) > currMax Then
        currMax = counts.Item(nums(i))
        currNum = nums(i)
    End If
Next 

num = currNum ' Most often found number
max = currMax ' Number of times it was found
C. Ross
This isn't homework, its for use at work.I know how to loop through the file, I just dont know how to see how many time a number appears.
Lasse
The if statement in the lower loop could be moved into the upper loop, that way you wouldn't have to loop through the file and then through the dictionary. Just check to see if counts.Item(arr(i)) > currMax after adding 1 to the counts.Item(arr(i))
Tester101
I prefer a clearer solution, especially when explaining.
C. Ross
A: 

hello i would go through the text and count the number of your nubmers. after that i would redim an dynamic array. - go throught the text from beginning to end, and store them in the array.

after that i would pick the first number, go through the array and count (for example in tmpcounter) the number of dublicates. [you could store the counted number from the textfile in tmphit]

the you pick the second number, count the number of dublicates ( tmpcounter2 /tmphit2)

compare the two counters,you "keep" the higher one and use the lowe one for the next number

...go on until the last field is validated.

at the end you know which number appearse most and how often.

i hope this help you. this is how i would programm it, maybe there is a better way or an API.

at the end you know

Tyzak
A: 

Try this

Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt",1)
Set dictNumbers = CreateObject("Scripting.Dictionary")

Dim MostKey
intHighest = -1

do while NOT objFile.AtEndOfStream
    LineArray = Split(objFile.ReadLine,";")
    for i = 0 to UBound(LineArray)
        if dictNumbers.Exists(LineArray(i)) Then
            dictNumbers.Item(LineArray(i)) = dictNumbers.Item(LineArray(i)) + 1
        else
            dictNumbers.Add LineArray(i), 1
        end if
        if dictNumbers.Item(LineArray(i)) > intHighest Then
            intHeighest = dictNumbers.Item(LineArray(i))
            MostKey = LineArray(i)
        end if
    next
Loop 

MsgBox MostKey
Tester101