Pick your data set. This works best for large groups, cases where you suspect that an entire class has the same copied answers. You'll be picking through all of the data, looking for specific similarities. You can use the same idea to compare only two students, but it's usually easier just to do this analysis by hand.
Now, construct your algorithm: for i = 1 to (end of test), you'll want to compare the first student's answer for # i to the second student's answer for # i, and so on.
Set up your "trapping" code. If student 1(i) = student 2(i), save the resulting identical answers to a text file.
Get the program to return "blocks" of identical answers. This is your key to discovering possible cheating activity.
Now look at the results. Finding examples of where a long string was answered identically by many students is evidence. Finding examples of where wrong and right answers were identical also indicates that cheating occurred.