fix algorithm to allow duplicates