473,398 Members | 2,113 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,398 software developers and data experts.

How to count value in a ArrayList

Hello!

I have an ArrayList with many double that is sorted some of these double
value exist more then once in the ArrayList.
Is it possible to use some feature in C# or from the framework to count each
unique double value or is the only
solution to run through the list and count every unique double value.

//Tony
Jun 27 '08 #1
18 3413
If you can use LINQ, then this page is an excelent resource
http://msdn.microsoft.com/en-us/vcsharp/aa336747.aspx.

Look at the example for Aggregate Operators - Count - Grouped.

"Tony" wrote:
Hello!

I have an ArrayList with many double that is sorted some of these double
value exist more then once in the ArrayList.
Is it possible to use some feature in C# or from the framework to count each
unique double value or is the only
solution to run through the list and count every unique double value.

//Tony
Jun 27 '08 #2
On Fri, 23 May 2008 13:43:17 +0200, Tony wrote:
Hello!

I have an ArrayList with many double that is sorted some of these double
value exist more then once in the ArrayList. Is it possible to use some
feature in C# or from the framework to count each unique double value or
is the only
solution to run through the list and count every unique double value.

//Tony
Maybe try using a hash table rather that an array list, searching of a
hash table is O(1) rather than a list O(n).

Regards j1mb0jay
Jun 27 '08 #3
On May 23, 12:43 pm, "Tony" <johansson.anders...@telia.comwrote:
I have an ArrayList with many double that is sorted some of these double
value exist more then once in the ArrayList.
Is it possible to use some feature in C# or from the framework to count each
unique double value or is the only
solution to run through the list and count every unique double value.
Leaving aside the other responses, I'd note that comparing double
values against each other directly is usually a bad thing. Two
sequences of calculations which are "logically" identical can have
different results, sometimes for very odd reasons to do with JIT
optimisations etc. It's usually better to compare within a certain
tolerance. What are these doubles representing, and where have they
come from? Different situations merit different approaches.

Jon
Jun 27 '08 #4
On Fri, 23 May 2008 04:43:17 -0700, Tony <jo*****************@telia.com>
wrote:
Hello!

I have an ArrayList with many double that is sorted some of these double
value exist more then once in the ArrayList.
Is it possible to use some feature in C# or from the framework to count
each
unique double value or is the only
solution to run through the list and count every unique double value.
And in addition to Jon's excellent point, I'll point out that if the list
is in fact sorted as you say, then you should use a binary search to find
the first instance of the value you're looking for. Then you only need to
examine from that point forward until you reach the next value not "equal"
(using whatever definition is appropriate).

For lists of any significant size, this would be _much_ more efficient
than enumerating the entire list.

Pete
Jun 27 '08 #5
On May 23, 7:43*am, "Tony" <johansson.anders...@telia.comwrote:
Hello!

I have an ArrayList with many double that is sorted some of these double
value exist more then once in the ArrayList.
Is it possible to use some feature in C# or from the framework to count each
unique double value or is the only
solution to run through the list and count every unique double value.

//Tony
Hi,

You will have to run through the list, there is no way to avoid that.
Jun 27 '08 #6
On Fri, 23 May 2008 12:11:48 -0700, Ignacio Machin ( .NET/ C# MVP ) wrote:
On May 23, 7:43Â*am, "Tony" <johansson.anders...@telia.comwrote:
>Hello!

I have an ArrayList with many double that is sorted some of these
double value exist more then once in the ArrayList. Is it possible to
use some feature in C# or from the framework to count each unique
double value or is the only
solution to run through the list and count every unique double value.

//Tony

Hi,

You will have to run through the list, there is no way to avoid that.
If you input the the data into a hash table rather than a ArrayList, that
will clone itself when it gets full and is rather slow. Then when you
want to look for an item you will only have to look at one item to see if
you have it, rather than the whole list.

Regards j1mb0jay
Jun 27 '08 #7
j1mb0jay wrote:
If you input the the data into a hash table rather than a ArrayList, that
will clone itself when it gets full and is rather slow. Then when you
want to look for an item you will only have to look at one item to see if
you have it, rather than the whole list.
Is that different from ArrayList ??

Arne
Jun 27 '08 #8
On Sat, 24 May 2008 11:01:49 -0400, Arne Vajhøj wrote:
j1mb0jay wrote:
>If you input the the data into a hash table rather than a ArrayList,
that will clone itself when it gets full and is rather slow. Then when
you want to look for an item you will only have to look at one item to
see if you have it, rather than the whole list.

Is that different from ArrayList ??

Arne
A well distributed hash table has a BigO(1) for all operations (Insert,
Search and Remove).

http://en.wikipedia.org/wiki/Hash_table

Regards j1mb0jay
Jun 27 '08 #9
j1mb0jay wrote:
On Sat, 24 May 2008 11:01:49 -0400, Arne Vajhøj wrote:
>j1mb0jay wrote:
>>If you input the the data into a hash table rather than a ArrayList,
that will clone itself when it gets full and is rather slow. Then when
you want to look for an item you will only have to look at one item to
see if you have it, rather than the whole list.
Is that different from ArrayList ??

A well distributed hash table has a BigO(1) for all operations (Insert,
Search and Remove).
So does an array list for operations at the tail.

Until the backing array is full.

And a hash table has the same issue.

Arne
Jun 27 '08 #10
On Sat, 24 May 2008 13:35:03 -0400, Arne Vajhøj wrote:
j1mb0jay wrote:
>On Sat, 24 May 2008 11:01:49 -0400, Arne Vajhøj wrote:
>>j1mb0jay wrote:
If you input the the data into a hash table rather than a ArrayList,
that will clone itself when it gets full and is rather slow. Then
when you want to look for an item you will only have to look at one
item to see if you have it, rather than the whole list.
Is that different from ArrayList ??

A well distributed hash table has a BigO(1) for all operations (Insert,
Search and Remove).

So does an array list for operations at the tail.

Until the backing array is full.

And a hash table has the same issue.

Arne
How can an array list have a O(1) Search, when it has to look at each
item and compare. Dont be so silly !!!

j1mb0jay
Jun 27 '08 #11
j1mb0jay wrote:
On Sat, 24 May 2008 13:35:03 -0400, Arne Vajhøj wrote:
>j1mb0jay wrote:
>>On Sat, 24 May 2008 11:01:49 -0400, Arne Vajhøj wrote:
j1mb0jay wrote:
If you input the the data into a hash table rather than a ArrayList,
that will clone itself when it gets full and is rather slow. Then
when you want to look for an item you will only have to look at one
item to see if you have it, rather than the whole list.
Is that different from ArrayList ??
A well distributed hash table has a BigO(1) for all operations (Insert,
Search and Remove).
So does an array list for operations at the tail.

Until the backing array is full.

And a hash table has the same issue.

How can an array list have a O(1) Search, when it has to look at each
item and compare. Dont be so silly !!!
Both inserting at the tail and removing from the tail is O(1).
Technically speaking searching at the tail is also O(1), but there
is not much point in that.

Arne
Jun 27 '08 #12
j1mb0jay wrote:
On Sat, 24 May 2008 18:31:49 -0400, Arne Vajhøj wrote:
>j1mb0jay wrote:
>>I was always taught to stay away from ArrayList at all costs.
You were taugth wrong.

ArrayList (or List<in newer .NET versions) is in most cases a very
good choice of data structure.

I thought the new ArrayList<TYPEwas just to stop unwanted items been
added item your list which in turn would be processed by your program
that maybe "harmful", and of course you no longer need to cast objects
when pulling them out structure.
It is more type safe.

Type safeness is a very good thing.

It also has a positive impact on performance of lists
of value types.
ArrayLists are simple to use and perfect
for most simple programs.
Also for complex programs.
Although you may argue that the more complex
programs be written in C of Assembly, for example Folding@Home.
It is the other way around. You can do simple programs in assembler
(and to some extent C). For complex programs you need a more
high level language.
I still stick with my argument, you do not have to re hash a hash table
when a large amount of collisions happen, you can just keep adding more
items, once a array list is full you have to clone it to a larger table.
You are free to stick with your argument.

But everybody can lookup in the .NET code and see that you are wrong -
the Hashtable/Dictionary also reallocates and copy data when it
is full.

Arne
Jun 27 '08 #13
On Sat, 24 May 2008 16:47:28 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
On Sat, 24 May 2008 16:20:12 -0700, j1mb0jay <j1******@uni.ac.ukwrote:
[...]
>I still stick with my argument, you do not have to re hash a hash table
when a large amount of collisions happen, you can just keep adding more
items,

You really should look at the actual classes in question before you make
that claim. There is a reason that the .NET hash table implementations
have a way to declare and inspect its "Capacity".
Sorry...my mistake. The capacity is internal, non-public. The docs refer
to it, but you can't access it directly.

Still, the basic point about the implementation remains. And as Arne
says, if you don't believe us, just look at the code.

Pete
Jun 27 '08 #14
j1mb0jay wrote:
Then
when you want to look for an item you will only have to look at one
item to see if you have it, rather than the whole list.
If a hash table is full of collisions, as you put it, it is in fact
just a list of linked lists, and not that much faster anymore. You must
most definitely look at more than one item, although of course only for
the bucket corresponding to the hash value you are looking for.

So rehashing is most definitely something that must be done, sometimes,
if you keep on having collisions, or if the list is rather full.

And reallocations in an ArrayList do not occur for each new item
either. If it is full, the capacity is doubled and the data is copied
over. From then on, you can add quite a few more items before it is
full.
--
Rudy Velthuis http://rvelthuis.de

"Your Highness, I have no need of this hypothesis."
-- Pierre Laplace (1749-1827), to Napoleon on why his works on
celestial mechanics make no mention of God.
Jun 27 '08 #15
On Sat, 24 May 2008 19:33:49 -0400, Arne Vajhøj wrote:
j1mb0jay wrote:
>On Sat, 24 May 2008 18:31:49 -0400, Arne Vajhøj wrote:
>>j1mb0jay wrote:
I was always taught to stay away from ArrayList at all costs.
You were taugth wrong.

ArrayList (or List<in newer .NET versions) is in most cases a very
good choice of data structure.

I thought the new ArrayList<TYPEwas just to stop unwanted items been
added item your list which in turn would be processed by your program
that maybe "harmful", and of course you no longer need to cast objects
when pulling them out structure.

It is more type safe.

Type safeness is a very good thing.

It also has a positive impact on performance of lists of value types.
> ArrayLists are simple to use and
perfect
for most simple programs.

Also for complex programs.
> Although you may argue that the more complex
programs be written in C of Assembly, for example Folding@Home.

It is the other way around. You can do simple programs in assembler (and
to some extent C). For complex programs you need a more high level
language.
>I still stick with my argument, you do not have to re hash a hash table
when a large amount of collisions happen, you can just keep adding more
items, once a array list is full you have to clone it to a larger
table.

You are free to stick with your argument.

But everybody can lookup in the .NET code and see that you are wrong -
the Hashtable/Dictionary also reallocates and copy data when it is full.

Arne
I never said anything about the .net version !!!

j1mb0jay
Jun 27 '08 #16
On Sat, 24 May 2008 17:58:45 -0700, j1mb0jay <j1******@uni.ac.ukwrote:
I never said anything about the .net version !!!
Well...

Whose version are you talking about then?

You can't make accurate statements about a hash table implementation
without knowing _which_ implementation you're talking about.

This being the C# newsgroup, the .NET hash table implementations certainly
seem like a reasonable assumption. If that's not the implementation
you're talking about, you need to be specific.

Pete
Jun 27 '08 #17
j1mb0jay wrote:
On Sat, 24 May 2008 19:33:49 -0400, Arne Vajhøj wrote:
>But everybody can lookup in the .NET code and see that you are wrong -
the Hashtable/Dictionary also reallocates and copy data when it is full.

I never said anything about the .net version !!!
There are not much point in posting about non-.NET in a
..NET newsgroup.

Arne
Jun 27 '08 #18
Tony wrote:
I have an ArrayList with many double that is sorted some of these double
value exist more then once in the ArrayList.
Is it possible to use some feature in C# or from the framework to count each
unique double value or is the only
solution to run through the list and count every unique double value.
You can not avoid the loop (with a list as data structure).

If you used List<doubleinstead of ArrayList you could use an
implicit loop as in:

int ndistinct = 0;
double lastx = double.NaN;
lst.ForEach(delegate(double x) { if(x != lastx) ndistinct++; lastx = x; });

But if you consider that nicer than a simple for loop is up to you.

And please pay attention to Jon's note about floating point
comparison.

Arne
Jun 27 '08 #19

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

Similar topics

0
by: kovac | last post by:
The System.directoryservices.dll has an error, and this error was described in http://support.microsoft.com/default.aspx?scid=kb;en-us;839424 At the moment we have Framework version v1.0.3705 and...
2
by: Niklas E | last post by:
Does anyone know how to get all nodes in a xml-document that start with a certain text? In my case I want to count all suppliers ( <id>Supplier*</id> ) <root> <customer> <name>ABC</name>...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
4
by: Krish | last post by:
I donot understand why it is this way. I have an arraylist. I understand I can get the count by arraylist.count in .net studio, intellisense also gives me arraylist.count.tostring()
2
by: Dan =o\) | last post by:
Hey guys, I'm getting really annoyed with this array... After selecting some items from a list view, I take the selected text from the first column and store it in an array, I then pass this...
12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
3
by: Eric Cathell | last post by:
I am using an arraylist to process data....the unique problem i have run into is that if I delete one of the indexed values the arraylist is reindexed and i get an exception thrown. I need to start...
5
by: colin | last post by:
some enumerable types like collections use Count but but other enumerable types like arrays use length why the difference? Im not sure what my function is going to be passed yet, some...
6
by: Slickuser | last post by:
Hi, I am picking up C#.net and I'm trying to add many values to one single key at different time in a loop. If the key already exist, append new value to previous? I'm not sure how to do that...
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
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
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
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.