calc problem... :D

DR00ME

Well-known member
Joined
Feb 6, 2004
Messages
169
Location
Finland
OK, lets say I got an Array which contains numbers from 0 to 255.....

like:

Dim MyArray(50) as byte

and it got values 4,4,4,7,6,3,230,100,2,1....n

now I want a code which calculate what number occur most... in this case it would be 4.

Or is there a function for this ?
 
I dont think theres anything built into .NET to do it, no. Do you have to use an Array? You can do some "nifty" stuff with a DataSet (it doesnt have to do database stuff) and its Compute and Select methods.

Writing code to do what you want is pretty trivial though, if you need it.

-nerseus
 
Theres no easy way to do that "in retrospective". You will have about 0.5*n^^2 operations if the array is of length (n). That sucks.

I recommend implementing an extra of object for this.

Code:
imports system.collections.specialized.
Class Counter

private mList as NameValueCollection
private mMax as integer = 0
private mMaxKey as string

public Sub Count (byVal Key as string)
   Dim curCount as integer
   if mList.contains(Key) then
      curCount = Convert.toInt32(mList(Key)) + 1
   else
      curCount = 1
   end if
   mList(Key) = CurCount

   if curCount > mMax then
      mMax = curCount
      mMaxKex = Key
  end if
end sub

Public Readonly Property MaxKey () as string
   Get
     return mMaxKey
   End Get
End Property

Public Readonly Property MaxCount () as integer
   Get
     return mMax
   End Get
End Property

Call the counter whenever a number is insertet to the array.
 
Last edited by a moderator:
Damn, love you guys lol :D Im gonna try it!

Im also interested what nerseus got in his mind... I think he got a few tricks for dataSet... If you have energy to could you please show me a little example of it without using an array ? Never used dataSet Im pretty new to vb.net anyway. I dont have to use an array neccessarily ....I might got one variable named x which is running some values between 0 and 255 so dont have to store em in an array.

P.S. There are over 70 000 numbers it is running between 0 and 255..... so basicly My array contains 70 000 numbers of type of byte and I have to find out which number occur most. But as I said earlier I do not have to store those 70 000 numbers in an array... just one variable going through those values.

P.S. nerseus whats the normal way to do it if I use an array ? and if Im not using an array (dataSet?) ?
 
Last edited by a moderator:
Originally posted by Heiko
Theres no easy way to do that "in retrospective". You will have about 0.5*n^^2 operations if the array is of length (n). That sucks.

I recommend implementing an extra of object for this.

Code:
imports system.collections.specialized.
Class Counter

private mList as NameValueCollection
private mMax as integer = 0
private mMaxKey as string

public Sub Count (byVal Key as string)
   Dim curCount as integer
   if mList.contains(Key) then
      curCount = Convert.toInt32(mList(Key)) + 1
   else
      curCount = 1
   end if
   mList(Key) = CurCount

   if curCount > mMax then
      mMax = curCount
      mMaxKex = Key
  end if
end sub

Public Readonly Property MaxKey () as string
   Get
     return mMaxKey
   End Get
End Property

Public Readonly Property MaxCount () as integer
   Get
     return mMax
   End Get
End Property

Call the counter whenever a number is insertet to the array.

"mList.contains(Key)"

I think there is no such a thing as .contains ?
it gives me error there.... did I miss something ?

also

curCount = Convert.toInt32(mList(Key)) + 1

why do you add 1 to the actual value ? or is the index added with 1 ? a bit confusing tough :D
 
Last edited by a moderator:
I wrote that down from memory. Sorry.

Try this.

Code:
Imports system.collections.specialized

Class Counter

Private mList As NameValueCollection
Private mMax As Integer = 0
Private mMaxKey As String

Public Sub Count (ByVal Key As String)

   Dim curCount As Integer

   Try
     Get the current number of elements for "key"
       curCount = Convert.ToInt32 (mList.Get(Key))
   Catch e as exception
      The Key was not in the list so far
      curCount = 0
    End try

    Now add one, because we want to add this key 
    another time

      curCount += 1

    Check if this is a new maximum
   If curCount > mMax Then
      mMax = curCount
      mMaxKex = Key
  End If
End Sub

Public ReadOnly Property MaxKey () As String
   Get
     Return mMaxKey
   End Get
End Property

Public ReadOnly Property MaxCount () As Integer
   Get
     Return mMax
   End Get
End Property

End class
 
There is a nicer solution
I dont know VB, but Ill write some pseude code that looks like VB
PHP:
Dim Counts(256) as int
FOR (i=0; i<256; i++)
     Counts[i] = 0;

FOR (i=0; i<MyArray.Length; i++)
    Counts[MyArray[i]] = Counts[MyArray[i]] + 1

int maxIndex = 0;
int maxCount = Counts[0];

FOR (i=1; i<Counts.Lenght; i++)
{
   IF (Counts[i] > maxCount)
   {
        maxIndex = i;
        maxCount = Counts[i];
    }
}

basically Counts will hold number of times specific number will occur. Since your values are up to 256 it will hold 256 elements and we initialize it to 0
Then we loop thru your array and add 1 to each occurance of a number. Then we find maximum index in Counts.
So for input:
[4,4,4,7,6,3,230,100,2,1]
Counts array will look like:
[0, 1, 1, 1, 3, 0, 1, 1, ...]

I dont know if VB arrays are 0 index or 1 indexed, if its one youd need to adjust the code.
 
Finally got it working ....it got as many operations as the array lenght * lenght :(
 
Last edited by a moderator:
Back
Top