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

compare a list of integers

Tem
List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);

List<intb = new List<int>();
b.Add(1);
b.Add(2);
b.Add(3);

I need to compare two integer lists.
It returns true if values and ordering are the same.
Is there a better way to do this than a foreach loop?

Tem
Jun 27 '08 #1
6 8455
Tem <te*****@yahoo.comwrote:
List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);

List<intb = new List<int>();
b.Add(1);
b.Add(2);
b.Add(3);

I need to compare two integer lists.
It returns true if values and ordering are the same.
Is there a better way to do this than a foreach loop?
Which version of .NET are you using? .NET 3.5 has an extension method
on IEnumerable<Tof SequenceEqual:

if (a.SequenceEqual(b))
....

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #2
Tem
3.5
but I thought extension methods would make things slow? been trying to avoid
them
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Tem <te*****@yahoo.comwrote:
>List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);

List<intb = new List<int>();
b.Add(1);
b.Add(2);
b.Add(3);

I need to compare two integer lists.
It returns true if values and ordering are the same.
Is there a better way to do this than a foreach loop?

Which version of .NET are you using? .NET 3.5 has an extension method
on IEnumerable<Tof SequenceEqual:

if (a.SequenceEqual(b))
...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #3
but I thought extension methods would make things slow? been trying to avoid
them
Why would you think that? OK, in reality you could shave a few cycles
if you know you have List<Tby checking the length first
(SequenceEqual doesn't do this, although this could be a missed
opportunity - it could check if both operands implement IList, and
compare the length) - and because it is using the interface (rather
than direct to the sealed List<T>) it won't inline any of the fetches
- but this is rarely the most significant thing.

My advice: use the simplest code that clearly expresses what you want
to do. SequenceEqual does this; *if* you later find this is a
bottleneck, then tweak it (perhaps introduce a List<TSequenceEqual
extension method) - but until then you're optimising prematurely.

Marc
Jun 27 '08 #4
For reference - an additional overload that checks a few obvious cases
first... just add your namespace as a "using" at the top, and the
overload resolution will select it instead of
Enumerable.SequenceEqual, since ICollection<Tis more specific than
IEnumerable<T>

Adding a List<T>-specific version would almost certainly be overkill;
I doubt you'd ever see any tangible performance benefit in most code.

Marc

namespace SomeNamespace
{
public static class CollectionExt
{
public static bool SequenceEqual<T>(this ICollection<Tfirst,
ICollection<Tsecond)
{
return SequenceEqual<T>(first, second,
EqualityComparer<T>.Default);
}
public static bool SequenceEqual<T>(this ICollection<Tfirst,
ICollection<Tsecond, IEqualityComparer<Tcomparer)
{
if (first == null) throw new
ArgumentNullException("first");
if (second == null) throw new
ArgumentNullException("second");
if (ReferenceEquals(first, second)) return true;
if (first.Count != second.Count) return false;
return System.Linq.Enumerable.SequenceEqual<T>(first,
second, comparer);
}
}
}
Jun 27 '08 #5
Tem
Thanks Marc

Something tells me this will be much more efficient.

"Marc Gravell" <ma**********@gmail.comwrote in message
news:98**********************************@24g2000h sh.googlegroups.com...
For reference - an additional overload that checks a few obvious cases
first... just add your namespace as a "using" at the top, and the
overload resolution will select it instead of
Enumerable.SequenceEqual, since ICollection<Tis more specific than
IEnumerable<T>

Adding a List<T>-specific version would almost certainly be overkill;
I doubt you'd ever see any tangible performance benefit in most code.

Marc

namespace SomeNamespace
{
public static class CollectionExt
{
public static bool SequenceEqual<T>(this ICollection<Tfirst,
ICollection<Tsecond)
{
return SequenceEqual<T>(first, second,
EqualityComparer<T>.Default);
}
public static bool SequenceEqual<T>(this ICollection<Tfirst,
ICollection<Tsecond, IEqualityComparer<Tcomparer)
{
if (first == null) throw new
ArgumentNullException("first");
if (second == null) throw new
ArgumentNullException("second");
if (ReferenceEquals(first, second)) return true;
if (first.Count != second.Count) return false;
return System.Linq.Enumerable.SequenceEqual<T>(first,
second, comparer);
}
}
}
Jun 27 '08 #6
Tem <te*****@yahoo.comwrote:
Something tells me this will be much more efficient.
Certainly in the special cases, where the two lists are either
"obviously" different or "obviously" the same.

But note that it's still an extension method - your earlier comment
about how you've been "trying to avoid [extension methods]" should be
reconsidered, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #7

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

Similar topics

30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
8
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
4
by: slot | last post by:
Is it possible to compare "enum" types to basic types like "unsigned short" type at all? Thanks!
49
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
2
by: Stevie | last post by:
Hello I'm trying to work out the regular expression for carrying out a simple 'greater than' comparison. I have a directory with files such as asd345.log, asd346.log, asd347.log and so on.
7
by: Girish Sahani | last post by:
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1 == ti2 and not index1 !=...
11
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is...
6
by: Amit Bhatia | last post by:
Hi, I am not sure if this belongs to this group. Anyway, my question is as follows: I have a list (STL list) whose elements are pairs of integers (STL pairs, say objects of class T). When I create...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.