To avoid comparing a question to itself, you could try to first check if the question's ID is not the same as the one it is being compared with. So, just before you start the comparision add something like
if ($question['question_id'] !== $compared_question['question_id']) {
// Your compare code
}
Can't really see in your code exactly where you should place it, but I hope you get the idea!