Molecular barcodes attached to the DNA make it possible to remove errors occurring during the library construction and sequencing. The process reduces the number of errors by up to one thousandfold compared to conventional sequencing, enabling detection of extremely rare variants even in the presence of a large background. In addition to removing errors, molecular barcoding allows the user to count the number of analyzed molecules which is important in applications such as circulation tumor DNA analysis and immune repertoire sequencing.