Obj format indexing algorithm

I have written a simple .obj format parser, but have trouble figuring out the fastest way to accomplish flattening 3 indices arrays into 1 which OpenGL requires. So far I import models in which each triangle has its own set of vertices, but for obvious reasons would want to avoid duplication, and use glDrawElements instead of glDrawArrays. FYI I’ve already integrated assimp, but want to have my own importer nevertheless. Can someone point me in the right direction?

Thanks in advance

Use a keyed map (dictionary, associative array, e.g. std::unordered_map in C++) to track the mapping from a tuple of (position, texture coordinates, normal) indices to the OpenGL index, and a linear array (e.g. std::vector) for the reverse mapping.

For each vertex of each face, you have a (v,vt,vn) tuple. If that tuple already exists in the map, store its associated OpenGL index in the element array. Otherwise, add a new entry to the map using the tuple as the key and the next unused OpenGL index (i.e. the .size() of the vector) as the value, and append the tuple to the linear array.

Once you’ve processed all of the faces, traverse the the linear array, appending the appropriate element from the v, vt and vn tables to the corresponding vertex array.

Thank you very much @GClements!