Diatomic molecules are molecules that contain only 2 atoms either of the same kind or different, for example
We know that the stability of a molecule is inversely proportional to the energy it possesses.
Oxygen and Hydrogen both exist in a diatomic state.
This is because hydrogen is much more stable when it does not combine with any other element, but itself.
The same is for Oxygen as well, it possesses less energy when it is uncombined, which means it is more stable.
The elements are much more stable when they are in an octet configuration.
As a result, to obtain the octet configuration, Oxygen shares its two electrons with another oxygen atom, and thus both atoms attain the octet state and become diatomic, .
In the same manner, Hydrogen shares its 1 electron with another hydrogen atom and attains a duplet state, and becomes diatomic, .
Hydrogen can have a duplet state because it has 1 electron that is , and the s-subshell can accommodate a maximum of 2 electrons.
Therefore due to this reason, Oxygen and Hydrogen are both diatomic.