
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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Sep 15, 2011, 1:37 AM EDT
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 RajaRank #861 · 2.6K reputationPosted Sep 15, 2011, 1:18 AM EDT
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"