473,698 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quickly finding duplicates in an ArrayList

Basically I have an ArrayList of strings; I need a fast way of
searching through and comparing the elements of the ArrayList and the
return an ArrayList of items that have 3 Duplicates.

For example, if the ArrayList has the following elements:

elems[0] = "blue"
elems[1] = "red"
elems[2] = "blue"
elems[3] = "green"
elems[4] = "red"
elems[5] = "red"

I want to have a function that goes through the list and then returns a
new arraylist like:

newElems[0] = "red"
newElems[1] = "red"
newElems[2] = "red"

In actuality my arraylist is an array of objects, which have a string
property and I'm referencing like: elems[0].filename = "red"; but the
above example is easier to follow.

I would like to be able to perform this search as fast as possible. I
personally don't have any real experience or familiarity with topics
such as binary trees, etc; would something like that be the route to
take? Thanks.

Nov 16 '05
11 33525
Yeah, I did google it immediately after posting the question and was
able to find sufficient information. If I could of deleted the post, I
would have. ;)

I do have a small question though. Does the .NET Hashtable work in
exactly the same manner as a general Hashtable? Also, I tried using
reflector to track down the GetHashCode function to see how it worked;
I ended up at the String class (taking into consideration the type of
key I was using was a string), and the code I found for the GetHashCode
function was:

[MethodImpl(Meth odImplOptions.I nternalCall)]
public override extern int GetHashCode();

I couldn't figure out where to find anymore info...

Nov 16 '05 #11
The GetHashCode method is a virtual member of an Object so everything
has a default implementation for it. The string's version of the
method is internal to the .NET framework. That's why you can't see it.
One of the most frequently used ways of getting the hash code of an
object is to XOR all of the object's fields. In the case of a string
you might XOR all characters together. I seriously doubt that's what
the framework is doing though. XOR works well because it is fast and
produces a very random distribution of values. That's important when
it comes time to map the hash code to a slot in the memory array. A
good distribution of hash codes lowers the probability that two keys
will get mapped to the same slot in memory. This is called a
collision. There are many strategies for handling collisions. I
believe the Hashtable in dotnet uses quadratic probing. This strategy
involves number theory and the use of prime numbers.

paradox wrote:
Yeah, I did google it immediately after posting the question and was
able to find sufficient information. If I could of deleted the post,
I would have. ;)

I do have a small question though. Does the .NET Hashtable work in
exactly the same manner as a general Hashtable? Also, I tried using
reflector to track down the GetHashCode function to see how it
worked; I ended up at the String class (taking into consideration
the type of key I was using was a string), and the code I found for
the GetHashCode function was:

[MethodImpl(Meth odImplOptions.I nternalCall)]
public override extern int GetHashCode();

I couldn't figure out where to find anymore info...


Nov 16 '05 #12

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

Similar topics

8
6979
by: Eric Linders | last post by:
Hi, I'm trying to figure out the most efficient method for taking the first character in a string (which will be a number), and use it as a variable to check to see if the other numbers in the string match that first number. I'm using this code for form validation of a telephone number. Previous records from the past few months show that when someone is just messing around on one of our forms (to waste our time), they type
3
4208
by: Erich | last post by:
I have a company table and I would like to write a query that will return to me any duplicate companies. However, it is a little more complicated then just matching on exact company names. I would like it to give me duplicates where x number of letters at the beginning of the company name match AND x number of letters of the address match AND x number of letters of the city match. I will be doing this in batches based on the first letter of...
0
1484
by: Timo Nentwig | last post by:
Hi! <node> <name>foo</name> <id>1</id> <age>7</age> </node> <node.....> <node> <name>foo</name>
1
1366
by: AlanAylett | last post by:
Hi all, i am having a problems with a SQL select query. I have to find all the records in a ONE table where TWO fields, field1 and field2 for example, are duplicated. eg field1 field2 a b
6
2890
by: Maxi | last post by:
I have 100 tabes in an Access database, every table has 1 filed with 100 names (records), no primary key assigned. I would like to find duplicates. Here is the criteria: The computer should pick up the first name of Table1 and check that name in that table (Table1) as well as the remaining 99 tables. Continue this till we reach the last name (record) of the 100th table.
5
8280
by: limperger | last post by:
Hello everyone! Is out there any way to search for duplicate entries without using the "Find duplicates" option? In the Access 97 installed in my workplace, the Find duplicates option is disabled (don't ask me why) and I think that there are little chances of having it installed. Any wonder of how to overcome this situation without the aforementioned wizard? Thank you very much in advance Best regards from Barcelona
17
2996
by: Gayatree | last post by:
I have to concatenate 2 colimns in a table and find duplicates in them. I already used the method select * from tableA a where (select count(*) from TableA b where acol1+ +col2 = b.col1+ +col2)>1 But the performance is very bad. Data is also huge Can you help me Thanks in advance
3
25072
Thekid
by: Thekid | last post by:
I'm trying to figure out a way to find if there are duplicates in an array. My idea was to take the array as 'a' and make a second array as 'b' and remove the duplicates from 'b' using 'set' and then compare a to b. If they're different then it will print out 'duplicates found'. The problem is that even after trying different arrays, some with duplicates some without, that 'b' rearranges the numbers. Here's an example: a='1934, 2311,...
0
8673
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8601
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3043
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
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.