I am using Visual C# 2010 Express. I have an array of students that tells me which pair of students have worked together in the past. So, arrStudents(0,0) might contain Joe and arrStudents(0,1) contains Bob, and sometimes vice versa. The order in which the names are recorded doesn’t matter, it’s just the pairing information that I care about.
Example:
Joe - Bob
Mary - Bob
Bob - Joe
Mary - Joe
Jack - Mary
Joe - Bob
What method should I use to identify all distinct pairs and to count how many times it occurred?
So, since (Joe – Bob) and (Bob – Joe) are the same pair the results should yield:
Joe - Bob, 3
Mary - Bob, 1
Mary - Joe, 1
Jack - Mary, 1
I’d create a
Dictionary, where the key is a student-pair and the value is the count. Then iterate through all the pairs and add them to the dictionary. For each pair, sort the names alphabetically (or whatever) so that both “Joe-Bob” and “Bob-Joe” end up as “Bob-Joe”. If the pair already exists in the dictionary, increment the corressponding value by 1. If it doesn’t, add it and set the value to 1. In the end, just iterate through the dictionary and you’ll have your results.