How can I remove all duplicates from a list except one?
(111222333) becomes (12333) or whatever...
partial unique?
Is this a list with sepearate one-digit integers?
Code: Select all
(unique '(2 3 4 4 6 7 8 7)) → (2 3 4 6 7 8)
Hans-Peter
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
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:
A non-iterative solution is easy too:
which returns the same thing.
Here's a simple list traversal:
Code: Select all
(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")
Code: Select all
(define (keep? e) (find e '("nobody"))) ;
(append (unique (clean keep? l)) (filter keep? l))