Shannon Holsinger Shannon Holsinger - 1 year ago 125
C# Question

Stat issue - comparing list against itself

This one is for you CompSci or stats people. Can you please tell me, if theList contains 72,786 "things," what the value of compareCount will be at the end of the loops? I'm thinking it's 72,786^2-1 but it's been soo long since this old brain worked like that. Much obliged for your time and assistance!

List<thing> theList = new List<thing>();//list contains 73,786 "things"
private void compare()
int compareCount = 0;
for(int i = 0; i < theList.Count-1; i++)
for(int comp = i + 1; comp < theList.Count; comp++)
compare(theList[i], theList[comp]);


Answer Source

The compareCount in your code will have the value (72786^2 - 72786) / 2 = 2648864505. I've confirmed that by running it. As it is written now, there is no need to have the call compare(theList[i], theList[comp]) in the inner loop (as it doesn't influence the count in any way).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download