So here is the method I learned years ago without memorizing an awkward formula. The concept is to reduce the molecular formula to a hydrocarbon and compare it to the most saturated hydrocarbon bearing the same number of carbon atoms.  Consider the infamous free-base, hydroxychloroquine, $\ce{C18H26ClN3O}$.<br><br>
 
<img src="https://i.sstatic.net/uuw2y.jpg" height="200"> <br><br>
The following atom replacements may be done in any order. Ignore divalent atoms ($\ce{O,S}$). The formula is now $\ce{C18H26ClN3}$. Replace all halogens with an equal number of hydrogens $\Rightarrow \ce{C18H27N3}$. Substitute $\ce{CH}$ for each nitrogen or trivalent atom $\Rightarrow \ce{C21H30}$. The most saturated $\ce{C21}$ hydrocarbon is $\ce{C21H44}$. Now do the math: $\ce{C21H44}$ - $\ce{C21H30}$ = $\ce{H14}$. Now divide 14 by 2. The Degree of Unsaturation is 7. <br><br>
As to the question as to why oxygen is dropped from the equation, if the oxygen in hydroxychloroquine weren't there, the degree of unsaturation would still be the same. For example, the Degrees of Unsaturation of tetrahydrofuran and cyclobutane are the same, D.U. = 1.