View Full Version : Removing duplicate strings in a list
whdjr
2004-06-09, 02:50 PM
Can someone refresh my memory as to what function is used to remove duplicate strings from a list.
I'm having a brain fart this morning.
Thanks,
Will
stig.madsen
2004-06-09, 03:24 PM
VL-REMOVE would probably be a good choice.
(vl-remove "hey" '("hey" "this" "is" "a" "bunch" "of" "heys"))
("this" "is" "a" "bunch" "of" "heys")
(vl-remove "heys" '("hey" "this" "is" "a" "bunch" "of" "heys"))
("hey" "this" "is" "a" "bunch" "of")
whdjr
2004-06-09, 03:30 PM
stig,
Thanks for the response. That worked too well I'm afraid.
Let me clarify what I need. I need to delete all the duplicates except one.
list would be :
("hey" "this" "is" "hey" "a" "bunch" "hey" "of" "heys")
remove all "hey" duplicates:
("hey" "this" "is" "a" "bunch" "of" "heys")
Does that clarify it a bit?
Thanks,
stig.madsen
2004-06-09, 04:31 PM
Hmmm, it clarifies but also makes it a bit more complex. How is the code supposed to know which item to keep? If the first match is to remain then perhaps something like this will do?
(defun removeAllButFirst (item lst / n ll)
(setq n (cdr (member item lst)))
(repeat (- (length lst) (length n))
(setq ll (cons (car lst) ll)
lst (cdr lst)))
(append (reverse ll) (vl-remove item n))
)
whdjr
2004-06-09, 05:59 PM
I just want it remove all items that are the same except one of them. It doesn't matter which one of them.
Thanks,
MarkTheSwampThomas
2004-06-09, 06:39 PM
I think you want this:
(vl-remove-if predicate-function list)
or:
(vl-remove-if-not predicate-function list)
stig.madsen
2004-06-09, 07:29 PM
Ah, no need for order, only removing duplicates:
(defun removeDups (item lst / ll)
(foreach n lst
(cond ((equal n item)
(and (not (member item ll))
(setq ll (cons n ll))))
(T (setq ll (cons n ll)))))
(reverse ll)
)
Ok, so it keeps the first occurrence like the former :)
Mark, just being curious: which predicate function would you write for VL-REMOVE-IF to remove duplicates?
whdjr
2004-06-09, 07:40 PM
Thanks stig I'll work that into my code and see how it works.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.