If you extract into an id-to-expression map instead of a list, you can then simply iterate over the map's surviving deduped elements.