fixing algorithm to consider thoses cases when all the elements happen with the same frequency