mode algorithm