How can I remove all duplicates from a list except one?
(111222333) becomes (12333) or whatever...
Is this a list with sepearate one-digit integers?
(unique '(2 3 4 4 6 7 8 7)) → (2 3 4 6 7 8)
its strings, and I want to keep specified duplicates.
("name" "name" "name" "nobody" "nobody" "nobody")
to
("name" "nobody" "nobody" "nobody")
Hi Tom. That's a rather quirky task, so I think you might have to write a bit more code. The exact answer might depend on whether you want to keep elements in the same order, etc.
Here's a simple list traversal:
(set 'l '("name" "name" "name" "nobody" "nobody" "nobody"))
(set 'keeplist '("nobody"))
(dolist (e l)
(if (find e keeplist)
(push e results -1))
(unless (find e results)
(push e results -1)))
;-> ("name" "nobody" "nobody" "nobody")
A non-iterative solution is easy too:
(define (keep? e) (find e '("nobody"))) ;
(append (unique (clean keep? l)) (filter keep? l))
which returns the same thing.