Make it two pass:
1) First do comparision, assigning new indices, marking vertex as no more used (for example setting it to [inf, inf, inf]).
2) Scan all your vertex array for unused vertices and either :
(a) compact it one-by-one -> move all vertices down one slot; scan all indices and if index is larger than cleaned vertex number - decrement int by 1.
(b) build list of unused vertices (while compacting them) and after this compact and reassign all indices in single pass.

(b) is probably harder to implement / debug, but I'm not sure: will it gain you significant performance increase?