Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two List<String> to each other? [duplicate]

Tags:

c#

.net

Let's say there are

List<string> a1 = new List<string>();  List<string> a2 = new List<string>(); 

Is there way to do like this?

if (a1 == a2)  {  } 
like image 887
Friend Avatar asked Mar 07 '12 13:03

Friend


People also ask

How do I compare two string lists?

Given two strings, represented as linked lists (every character is a node in a linked list). Write a function compare() that works similar to strcmp(), i.e., it returns 0 if both strings are the same, 1 if the first linked list is lexicographically greater, and -1 if the second string is lexicographically greater.

How do I compare two lists for duplicates in Python?

We can club the Python sort() method with the == operator to compare two lists. Python sort() method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.

Can I use == to compare two strings?

The == operator, known as the equality operator, is used to compare two strings in Java.

How do you compare two lists of strings in Python?

Use sort() method and == operator to compare lists The sorted list and the == operator are used to compare the list, element by element.


1 Answers

If you want to check that the elements inside the list are equal and in the same order, you can use SequenceEqual:

if (a1.SequenceEqual(a2)) 

See it working online: ideone

like image 127
Mark Byers Avatar answered Oct 01 '22 19:10

Mark Byers