473,586 Members | 2,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find the most repeating item

Hi,

I have an array/arraylist of string items. What I want is to find out which
item repeats itself most often. Any simple example for that? Thanks in
advance.

sean
Nov 21 '05 #1
2 1296
Shen wrote:
Hi,

I have an array/arraylist of string items. What I want is to find out which item repeats itself most often. Any simple example for that? Thanks in advance.


Use an implementation of IDictionary (I use a HashTable here) to enable
the use of strings as keys:

Public Function ArrayMode(ByVal Strings() As String) As String
Dim ht As Hashtable = New Hashtable(Strin gs.Length \ 2)
Dim s As String, occurrences As Integer
Dim ModalString As String, ModalOccurrence s As Integer = 0

For Each s In Strings
If ht.Contains(s) Then
occurrences = CInt(ht.Item(s) )
occurrences += 1
ht.Item(s) = occurrences
Else
occurrences = 1
ht.Add(s, occurrences)
End If

If occurrences > ModalOccurrence s Then
ModalOccurrence s = occurrences
ModalString = s
End If
Next

Return ModalString
End Function
If you want to accept an ArrayList, change the first two lines to
Public Function ArrayMode(ByVal Strings As ArrayList) As String
Dim ht As Hashtable = New Hashtable(Strin gs.Count \ 2)
No guarantees that this is the best way :) For example, you could have
the routine notice that if a given string ever gets more than n/2
votes, it will definitely be the winner, so there is no point checking
the rest.

--
Larry Lard
Replies to group please

Nov 21 '05 #2
ms
Thanks a lot Larry. I'll give a try.
"Larry Lard" <la*******@hotm ail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Shen wrote:
Hi,

I have an array/arraylist of string items. What I want is to find out

which
item repeats itself most often. Any simple example for that? Thanks

in
advance.


Use an implementation of IDictionary (I use a HashTable here) to enable
the use of strings as keys:

Public Function ArrayMode(ByVal Strings() As String) As String
Dim ht As Hashtable = New Hashtable(Strin gs.Length \ 2)
Dim s As String, occurrences As Integer
Dim ModalString As String, ModalOccurrence s As Integer = 0

For Each s In Strings
If ht.Contains(s) Then
occurrences = CInt(ht.Item(s) )
occurrences += 1
ht.Item(s) = occurrences
Else
occurrences = 1
ht.Add(s, occurrences)
End If

If occurrences > ModalOccurrence s Then
ModalOccurrence s = occurrences
ModalString = s
End If
Next

Return ModalString
End Function
If you want to accept an ArrayList, change the first two lines to
Public Function ArrayMode(ByVal Strings As ArrayList) As String
Dim ht As Hashtable = New Hashtable(Strin gs.Count \ 2)
No guarantees that this is the best way :) For example, you could have
the routine notice that if a given string ever gets more than n/2
votes, it will definitely be the winner, so there is no point checking
the rest.

--
Larry Lard
Replies to group please

Nov 21 '05 #3

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

Similar topics

11
4143
by: Christoph Boget | last post by:
When building a form using Infopath, you can define a repeating section and stick form fields in that section. I'm curious if ASP.NET has a similar control to make it easy to design something similar using just ASP.NET (and not Infopath)? I'd hate to think that I'll need to write all the javascript/dhtml to mimic that functionality and I...
2
6613
by: nickheppleston | last post by:
I'm trying to iterate through repeating elements to extract data using libxml2 but I'm having zero luck - any help would be appreciated. My XML source is similar to the following - I'm trying to extract the line number and product code from the repeating line elements: <order xmlns="some-ns"> <header> <orderno>123456</orderno> </header>
1
1315
by: PaulF | last post by:
I am trying to do add a repeating XmlNode into and existing XML document and have had some problems. The base XML: <Property> <Premises> <Endorsement> <ShortWording/> <Wording/>
1
3436
by: mark4asp | last post by:
Apologies, I just can't get my head around xslt but I need to do this. I have an xml file with two attributes per product. One of the attributes repeats to produce several groups (3 in the example, grouped by region). How can I select this repeating attribute out as a header column in a table with the other attribute as in a data column....
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.