Simplest way: sort and go through just one time