Sonar can help improve the quality of code by detecting duplicated or cloned code, which can lead to bugs, increased maintenance costs, and decreased productivity. Duplicated code can be created through various means, including copying and pasting, reusing existing code without modification, generating code with tools, and lack of abstraction mechanisms in programming languages. Sonar has its own library called sonar-cpd to detect duplicated code, which offers improved performance, accuracy, and control compared to previous methods. Additionally, Sonar can now detect cross-project duplications, providing more opportunities for abstracting and mutualizing code.