DR00ME Posted February 20, 2004 Posted February 20, 2004 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 ? Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
*Experts* Nerseus Posted February 20, 2004 *Experts* Posted February 20, 2004 I don't think there's 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 doesn't 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Heiko Posted February 20, 2004 Posted February 20, 2004 (edited) There's 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. 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. Edited February 20, 2004 by Heiko Quote .nerd
DR00ME Posted February 20, 2004 Author Posted February 20, 2004 (edited) Damn, love you guys lol :D I'm 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 I'm 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 don't 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 what's the normal way to do it if I use an array ? and if I'm not using an array (dataSet?) ? Edited February 20, 2004 by DR00ME Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
DR00ME Posted February 20, 2004 Author Posted February 20, 2004 (edited) There's 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. 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 Edited February 20, 2004 by DR00ME Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
Heiko Posted February 20, 2004 Posted February 20, 2004 I wrote that down from memory. Sorry. Try this. 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 Quote .nerd
Werdna Posted February 20, 2004 Posted February 20, 2004 There is a nicer solution I don't know VB, but I'll write some pseude code that looks like VB 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 don't know if VB arrays are 0 index or 1 indexed, if it's one you'd need to adjust the code. Quote
DR00ME Posted February 20, 2004 Author Posted February 20, 2004 (edited) hmm I think the index of Counts goes out of range ? Edited February 20, 2004 by DR00ME Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
DR00ME Posted February 20, 2004 Author Posted February 20, 2004 (edited) Finally got it working ....it got as many operations as the array lenght * lenght :( Edited February 20, 2004 by DR00ME Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.