
I have a list of string type. i need to compare all the elements and judge whether they are equal or not?

Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Sep 15, 2011, 5:37 AM
bool areEqual = true;
if (list->Count > 0)
{
String ^ first = list[0];
for (int i = 1; i < list->Count; i++)
{
if (list[i] != first)
{
areEqual = false;
break;
}
}
}
if(areEqual)
{
// do something
}
else
{
// do something else
}
Prabhu RajaPosted Sep 15, 2011, 5:18 AM
Try this
public static bool IsUniqueFast(List
{
List
arrCopy.Sort(); // Sort Before you Compare
for (int i = 0; i < arrCopy.Count-1; i++)
{
if (arrCopy[i].Equals(arrCopy[i + 1]))
return false;
}
return true;
}
----------------------------------------
If this helps you, then mark as "Correct Answer"