rewrite of firstUnequalIndex