Use array instead of simple vars.
Simplify isDuplicateExists().
Refactor to remove copy-paste.