Your second loop will look almost like the first pseudo code loop I posted above but instead of adding to the hashed data structure you do an index look-up. The details depend on what data structure you use but here is some pseudo code again:
Code:
// first loop
vertices = set()
foreach triangle:
    foreach vertex of triangle:
        vertices.add(vertex.x, vertex.y, vertex.z, vertex.nx, vertex.ny, vertex.nz, vertex.u, vertex.v)

// second loop
indices = array()
foreach triangle:
    foreach vertex of triangle:
        indices.add(vertices.indexof(vertex.x, vertex.y, vertex.z, vertex.nx, vertex.ny, vertex.nz, vertex.u, vertex.v))

I hope you get the idea now?

I think it also would be possible to do the whole job in one loop but then it gets a bit more complicated and I don't think it would have much performance advantage.