devitg Posted October 15, 2021 Share Posted October 15, 2021 Hi all, this defun removes duplicated in a list (DEFUN REMOVE-DUPS (LISTE / RETLISTE) ;_ 01 (FOREACH ITEM LISTE (IF (NOT (MEMBER ITEM RETLISTE)) (SETQ RETLISTE (CONS ITEM RETLISTE)) ) ) (REVERSE RETLISTE) ) ;;******************************************************************************************************* (REMOVE-DUPS (list 1 2 2 3 4 4 5 6 6 6 )) ;(1 2 3 4 5 6) But I need to have the repeated like ( 2 2 4 4 6 6 6) or at least ( 2 4 6 ) some like (defun get-repeated ( list /) ) Quote Link to comment Share on other sites More sharing options...
mhupp Posted October 15, 2021 Share Posted October 15, 2021 (edited) Probably a better way to do this. but this will get what you need. (DEFUN Dup-count (lst / i c a Dup-lst lstx x) (setq i -1) (foreach x lst (setq a 0) (setq c 0) (repeat (length lst) (if (equal x (nth a lst)) (setq c (1+ c)) ) (setq a (1+ a)) ) (setq i (1+ i)) (if (not(member (cons x c) Dup-lst)) (if (> c 1) (setq Dup-lst (cons (cons x c) Dup-lst)) ) ) ) (foreach x Dup-lst (repeat (cdr (assoc (car x) dup-lst)) (setq lstx (cons (car x) lstx)) ) ) lstx ) the dup-lst has the items + the number of times its in the list like this. (2 . 3) I don't know how to pull the 2nd number from that (cadr x) doesn't seem to work. figured it out need to use assoc (dup-count '(1 2 2 2 3 4 5 6 6 6 7 8 9 9 9)) (2 6 9) (2 2 2 6 6 6 9 9 9) Edited October 15, 2021 by mhupp added repeat Quote Link to comment Share on other sites More sharing options...
Tharwat Posted October 15, 2021 Share Posted October 15, 2021 Here is my attempt. (defun Get:Duplicates ( l / f n) (mapcar '(lambda (x) (and (setq f (member x l)) (member x (cdr f)) (or (member x n) (setq n (cons x n)))) ) l) (reverse n) ) 1 Quote Link to comment Share on other sites More sharing options...
mhupp Posted October 15, 2021 Share Posted October 15, 2021 (edited) 3 minutes ago, Tharwat said: Here is my attempt. like i said better ways to do it! so much cleaner. really need to sit down and learn mapcar and lambda. Edited October 15, 2021 by mhupp Quote Link to comment Share on other sites More sharing options...
BIGAL Posted October 16, 2021 Share Posted October 16, 2021 Maybe something I found recently and using. ; By Gile (defun remove_doubles (lst) (if lst (cons (car lst) (remove_doubles (vl-remove (car lst) lst))) ) ) Then a VL-sort ? (setq lst (vl-sort lst '(lambda (x y) (< (car x)(car y))))) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 16, 2021 Share Posted October 16, 2021 (edited) Recursive - (defun f ( l ) (if l (if (vl-position (car l) (cdr l)) (cons (car l) (f (vl-remove (car l) (cdr l)))) (f (cdr l)) ) ) ) Iterative - (defun g ( l / r x ) (while (setq x (car l) l (cdr l)) (if (vl-position x l) (setq r (cons x r) l (vl-remove x l)) ) ) (reverse r) ) Edited October 16, 2021 by Lee Mac 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.