Algorithm implemented for duplicated keys and values