What you need to do is to analyze the set of input vertices (each triangle contributes three of these) and assign consecutive indices to them so that vertices that appear multiple times (i.e. are shared by different triangles) are assigned the same index every time. This leaves you with a list of (unique) vertices ${\bf x}_i$, and a list of index triples $(i, j, k)$ indexing into the vertex list so that the $({\bf x}_i, {\bf x}_j, {\bf x}_k)$'s represent the original triangles.
In principle this is easy to do - just use a hash table to associate indices to 3D points and fill it as you iterate over all vertices.
In practice, the coordinates of equivalent vertices will possibly not be exactly identical but differ by small amounts due to roundoff problems. In this case, discretize your input data with a grid resolution of $\delta$ (which should be a small number corresponding to the maximum expected roundoff error, e.g. $10^{-6}$). Use the grid coordinates of your vertices thus obtained (rather than their 3D positions) as keys in your hashtable to find the associated vertex index, but make sure to check whether any one of the neighboring grid cells was assigned an index earlier before generating a new index. The last step ensures that close vertices will receive the same index.