See CopyAndPasteProgramming.

Use PeeEmDee as a tool to detect CodeDuplication.

RefactorMercilessly until PeeEmDee detects no code duplication.

See CpdTool or go directly to http://pmd.sourceforge.net/cpd.html