Brominated and/or chlorinated organic compounds (referred to as organohalogens) are frequently detected in natural and engineered environments. However, ultrahigh-resolution mass spectrometry (UHR-MS)-based nontargeted identification of organohalogens remains challenging because of the coexistence of a vast number of halogenated and nonhalogenated organic molecules. In this study, a new algorithm, namely, the NOMDBP code, was developed to simultaneously identify organohalogens and non-organohalogens from the UHR-MS spectra of natural and engineered waters. In addition to isotopic patterns, for the first time, three optional filter rules [i.e., selection for minimum nonoxygen heteroatoms, inspection of the presence of newly formed halogenated disinfection byproducts (Xn-DBPs), and of their precursors] were incorporated into our code, which can accurately identify DBP-associated peaks and further elucidate Xn-DBP generation and transformation mechanisms. The formula assignment ratio against 2815 previously reported organohalogens, and their 11,583 isotopologues exceeded 97%. Application of our algorithm to disinfected natural organic matter indicated that oxygen-containing Xn-DBP species accounted for a majority of the Xn-DBPs. Furthermore, brominated Xn-DBPs (Br-DBPs) were characterized by a higher degree of unsaturation compared to chlorinated Xn-DBPs. In addition to electrophilic substitution and electrophilic addition reactions, the decomposition/transformation pathway was found to be another important mechanism in Br-DBP formation. The results of this study highlight the superior potential of our code for the efficient detection of yet unknown organohalogens (including organohalogens bearing nonoxygen heteroatoms) in a nontargeted manner and for the identification of their generation mechanism occurring during the disinfection process.