Jump to content

To get repeated in a list


devitg

Recommended Posts

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  /)




)

 

 

 

 

 

 

 

Link to comment
Share on other sites

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 by mhupp
added repeat
Link to comment
Share on other sites

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)
  )

 

  • Like 1
Link to comment
Share on other sites

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 by mhupp
Link to comment
Share on other sites

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)))))

 

Link to comment
Share on other sites

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 by Lee Mac
  • Like 1
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...