Skip to content

Instantly share code, notes, and snippets.

@dyoo
Last active December 11, 2015 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dyoo/4679038 to your computer and use it in GitHub Desktop.
Save dyoo/4679038 to your computer and use it in GitHub Desktop.
Finding duplicates
#lang racket
(define (find-duplicates elts)
(define ht (make-hash))
(for/fold ([dups '()]) ([x (in-list elts)])
(hash-set! ht x (add1 (hash-ref ht x 0)))
(cond
[(= (hash-ref ht x) 2)
(cons x dups)]
[else
dups])))
(find-duplicates '(1 2 2 3 1 4 5 4 4 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment