Finding sequence of pairs with second element of previous pair matching first element of next pair
The integers of your problem form nodes, the pairs form directed edges. Then you can use Tarjan's strongly connected components algorithm to find the groups of nodes that form a cycle.