473,396 Members | 1,766 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,396 software developers and data experts.

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 1281
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(Strings.Length \ 2)
Dim s As String, occurrences As Integer
Dim ModalString As String, ModalOccurrences 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 > ModalOccurrences Then
ModalOccurrences = 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(Strings.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*******@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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(Strings.Length \ 2)
Dim s As String, occurrences As Integer
Dim ModalString As String, ModalOccurrences 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 > ModalOccurrences Then
ModalOccurrences = 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(Strings.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
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...
2
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...
1
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
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
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,...

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.