Version without using frequencies function, for learning sake