The question assumes a set theory with _urelements_ (or _fundamental elements_ ). Given a set $\,S\,$ we want to find $\,{\scr U}(S),\,$ the set of all the urelements used in the construction of $\,S.\,$ The definition is recursive as follows. If $\,S\,$ is an urelement, then $\,{\scr U}(S) = S,\,$ or else, $\,{\scr U}(S) = \bigcup_{x\in S} {\scr U}(x).\,$ With a recursive definition you don't need to know the depth of recursion in advance.
Of course, you need some kind of axiom of foundation to ensure that the recursion "bottoms out". It is needed to eliminate the possibility of having "sets" such as $$S_0 := \\{S_1\\},\, S_1 := \\{S_2\\},\, \dots,\, S_n := \\{S_{n+1}\\},\, \dots. \tag{1}$$ Note the similarity to $$S_1 := \\{S_0\\},\, S_2 := \\{S_1\\},\, \dots,\, S_{n+1} := \\{S_n\\},\, \dots, \tag{2}$$ but they are very different cases when used with $\,{\scr U}(S_n).\,$