misra gries algorithm implementation