473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Numbers & Numbers

I have an array filled with counts of numbers. Example:

ar(1) contains the number of occurrances of the number 1
ar(2) contains the number of occurrances of the number 2
etc...

I need to find the 10 numbers with the highest occurrances.

I must be overlooking something but i've been beating my head against the
wall for a couple of hours now playing with arrays. Someone point me in the
right direction?
Nov 12 '07 #1
2 1248
On Nov 12, 1:35 am, "Terry Olsen" <tolse...@hotmail.comwrote:
I have an array filled with counts of numbers. Example:

ar(1) contains the number of occurrances of the number 1
ar(2) contains the number of occurrances of the number 2
etc...

I need to find the 10 numbers with the highest occurrances.

I must be overlooking something but i've been beating my head against the
wall for a couple of hours now playing with arrays. Someone point me in the
right direction?
This seems like huge overkill, but if I understood your problem it's
the first thing that came to mind. Basically, I transformed the
original array into a list of KeyValue pairs (to maintain the index in
the original array), sorted the List with a custom comparer, and
turned it back into an array. The following is a Console project, just
copy and paste it in and you'll see what I mean:

////////////////////////
Module Module1

Sub Main()
'// Demo array
Dim array As Integer() = {73, 5, 13, 41, 2, 4, 53, 42, 14, 15,
16, 67}

'// Our List
Dim list As New List(Of KeyValuePair(Of Integer, Integer))

'// Move the array to the list
For i As Integer = 0 To array.Length - 1
list.Add(New KeyValuePair(Of Integer, Integer)(i,
array(i)))
Next

'// Sort the List by Values
list.Sort(New MyComparer())

'// Move the List back to an Array
Dim newArray As KeyValuePair(Of Integer, Integer)() =
list.ToArray()

'// Print the top 10 results
For i As Integer = 0 To 9
Console.WriteLine("{0} with {1} occurrances",
newArray(i).Key, newArray(i).Value)
Next

Console.Read()

End Sub
End Module

Public Class MyComparer
Implements IComparer(Of KeyValuePair(Of Integer, Integer))

Public Function Compare(ByVal x As
System.Collections.Generic.KeyValuePair(Of Integer, Integer), ByVal y
As System.Collections.Generic.KeyValuePair(Of Integer, Integer)) As
Integer Implements System.Collections.Generic.IComparer(Of
System.Collections.Generic.KeyValuePair(Of Integer, Integer)).Compare
If x.Value = y.Value Then
Return 0
ElseIf x.Value < y.Value Then
Return 1
Else
Return -1
End If
End Function

End Class
////////////////////////

Thanks,

Seth Rowe

Nov 12 '07 #2
I have an array filled with counts of numbers. Example:
ar(1) contains the number of occurrances of the number 1
ar(2) contains the number of occurrances of the number 2
etc...
I need to find the 10 numbers with the highest occurrances.
Dim ir(UBound(ar)) As Integer
For i As Integer = LBound(ir) To UBound(ir) : ir(i) = i : Next
Array.Sort(ar, ir)

If n is ubound(ar), then ar(n) is the max frequency, and ir(n) is the number
having that frequency. ar(n-1) is the 2nd highest frequency, and ir(n-1) is
the number having that frequency. And so on.

As written, ar() is destroyed. If you need to preserve it, you should sort
an ar() clone.

Nov 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peter | last post by:
I've just completed my first (useful) VB6 project & created an exe file. On of the options was creating a version number - fine but how does my program get at this to display it? I also posted a...
9
by: Bart Nessux | last post by:
I am using method 'a' below to pick 25 names from a pool of 225. A co-worker is using method 'b' by running it 25 times and throwing out the winning name (names are associated with numbers) after...
1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
4
by: John Bowman | last post by:
Hi, A couple Q's , so I've come to the experts<g>. 1) I need a definitive answer to the following debate. I've got a couple developers who disagree on the following question. We've got an XML...
0
by: Kevin | last post by:
Group By - Client Sum - Past:IIF(DateDiff("d",,Date())>30,,0) Sum - Current:IIF(DateDiff("d",,Date())<31,,0) Works for positive numbers, but if InvoiceNetTotal = ($4.00) and InvoiceNetTotal =...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
3
by: dungdang | last post by:
I have a file that contains a single colum per row, about 20,000 rows like : 2345607 2342374 2434522 1432334 1326514 7768518 7612364
2
by: Blaze | last post by:
How do i display a table of prime numbers in c++ between "starting number" and "Finishing Number"?
4
by: BoscoPippa | last post by:
Hi all, I'm a rank beginner to C++ and programming in general. I'm in week 6 of my first course, and we have an assignment I'm having a little trouble with. If it matters, we're using standard...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.