Finalised initial working algorithm for duplicates()