correct the conditions

else there is infinite loop if all equal keys