How to count duplicate items in ArrayList?

O

Ook

Say I have ArrayList<String>. Is there a function that will count duplicate
occurences of a specific item? I can do it with just a few lines of code,
but is there already something that will do that?
 
L

lavanya_y2k

u can not count how duplictae items but u can find out whether element
alreday exist r nt using
contains() method. if there z any other ways 2 count elements please
let me know
 
D

deepak

Hi,
you can count it by using the collections framework .there is a
method in Collections that is frequency which count the frequence of
particular object.you can iterate this mathod throw loop.
this is the most easy way which i know.
 
E

Eric Sosman

Ook said:
Say I have ArrayList<String>. Is there a function that will count duplicate
occurences of a specific item? I can do it with just a few lines of code,
but is there already something that will do that?

It depends on what you mean by "duplicate occurrence." If
two String objects that are distinct but have identical content
count as "duplicates," you could build a HashSet<String> from
your given collection and then ask for its size. If you mean
"duplicates" as "references to the exact same instance," you
could do a similar thing with IdentityHashMap<String,Object>
(using any arbitrary value -- null, say -- for the "value").
Either way, subtract the size() of the resulting collection
from the size() of the original list to count the duplicates.

If the original ArrayList is short, the obvious nested-loop
solution is probably better. But that's O(N*N), so for longer
lists you'll want to use the heavier artillery.

Postscript: Now I'm starting to wonder what you mean by
"a specific item." Perhaps you just want to know how many
times the String "Rover" appears in the list, not the fact
that "Rover" appears twice and "Fido" three times, for three
duplicates altogether. As far as I know there's no built-in way
to do the search -- maybe there's something you could do with
set intersection and set difference, but that sounds like
overkill. Just iterate over the list and test each element
with equals() or == as appropriate, keeping a count of hits.
But if you're going to perform a lot of searches like this, it
may be time to take a step back and ask whether ArrayList is
the right data structure for the job: Maybe you need a Map or
perhaps a SortedSet instead.
 
L

Lew

Eric said:
But if you're going to perform a lot of searches like this, it
may be time to take a step back and ask whether ArrayList is
the right data structure for the job: Maybe you need a Map or
perhaps a SortedSet instead.

For example, you could use a Map< String, Integer>, where the Integer
represents the number of occurrences of the String. This could be called a
"concordance".

- Lew
 
E

Eric Sosman

Lew said:
For example, you could use a Map< String, Integer>, where the Integer
represents the number of occurrences of the String. This could be called
a "concordance".

Hmmm... I'd have called it a "histogram." To me, the word
"concordance" suggests Map<String,Set<ChapterAndVerse>>.

Nomenclature aside, the O.P. may want to reconsider whether
he's chosen the Collection that's best-suited to his problem,
whatever that might be.
 
O

Ook

Eric Sosman said:
It depends on what you mean by "duplicate occurrence." If
two String objects that are distinct but have identical content
count as "duplicates," you could build a HashSet<String> from
your given collection and then ask for its size. If you mean
"duplicates" as "references to the exact same instance," you
could do a similar thing with IdentityHashMap<String,Object>
(using any arbitrary value -- null, say -- for the "value").
Either way, subtract the size() of the resulting collection
from the size() of the original list to count the duplicates.

If the original ArrayList is short, the obvious nested-loop
solution is probably better. But that's O(N*N), so for longer
lists you'll want to use the heavier artillery.

Postscript: Now I'm starting to wonder what you mean by
"a specific item." Perhaps you just want to know how many
times the String "Rover" appears in the list, not the fact
that "Rover" appears twice and "Fido" three times, for three
duplicates altogether. As far as I know there's no built-in way
to do the search -- maybe there's something you could do with
set intersection and set difference, but that sounds like
overkill. Just iterate over the list and test each element
with equals() or == as appropriate, keeping a count of hits.
But if you're going to perform a lot of searches like this, it
may be time to take a step back and ask whether ArrayList is
the right data structure for the job: Maybe you need a Map or
perhaps a SortedSet instead.

select count(*) as nCount from <table> group by <field>

would give me exactly what I want. It's a short ArrayList, maybe 500
elements, and I need to know how many times each string is duplcated. How
many "dog", how many "cat", etc.

Map<String,int> would be ideal for another part of this project, but I'm
using a class with a string and int because thats how I did it in c++ and I
don't know java well enough to make the Map work (it's a school project).
 
P

Patricia Shanahan

Ook wrote:
....
select count(*) as nCount from <table> group by <field>

would give me exactly what I want. It's a short ArrayList, maybe 500
elements, and I need to know how many times each string is duplcated. How
many "dog", how many "cat", etc.

Map<String,int> would be ideal for another part of this project, but I'm
using a class with a string and int because thats how I did it in c++ and I
don't know java well enough to make the Map work (it's a school project).

Are you allowed to use exact code given to you by someone else? I need
to do something like this often enough that I have a utility class to do it.

Patricia
 
Z

zootal

How do you do this? I've looked at the docs and searched, and I can't
find anything explaining how to use this "frequency" method.
 
Z

zootal

Are you allowed to use exact code given to you by someone else? I need
to do something like this often enough that I have a utility class to do it.

Patricia

How complex is the utility class? I'd love to see it, even if I can't
use it.
 
O

Ook

How complex is the utility class? I'd love to see it, even if I can't
use it.

Ahhhh!!! Friggin' google groups! It posted my email address. Dang, another
good email address shot to crud :(
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ook schreef:
select count(*) as nCount from <table> group by <field>

would give me exactly what I want. It's a short ArrayList, maybe 500
elements, and I need to know how many times each string is duplcated. How
many "dog", how many "cat", etc.

Map<String,int> would be ideal for another part of this project, but I'm
using a class with a string and int because thats how I did it in c++ and I
don't know java well enough to make the Map work (it's a school project).

Have a look at Commons Collections’ Bag interface and implementations:
http://jakarta.apache.org/commons/c.../commons/collections/bag/package-summary.html

HTH, H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFtJtye+7xMGD3itQRAsHaAJ9ncyvMNMcdbW1/JAnDLE0ZhnCixQCfe5Hu
+gSlOsagigYzC2itCOhjiXA=
=CDqP
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top