• Re: duplicates

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Thu Jun 26 18:31:53 2025
    From Newsgroup: comp.lang.lisp

    B. Pym wrote:

    Pascal Costanza wrote:

    There doesn't seem to be a way to return a list of duplicates of a sequence in ANSI CL -- though there is a remove-duplicates. Is there a reason for this? It would be handy if you could tell remove-duplicates not to include any duplicated elements so you could do a set-
    difference at the end to get a list of duplicates. Feel free to post
    code to prove me wrong. Thanks!

    (loop
    with counts
    for element in list
    do (incf (getf counts element 0))
    finally (return
    (loop for (element count) on counts by #'cddr
    if (> count 1)
    collect element into duplicates
    else collect element into uniques
    finally (return (values uniques duplicates)))))

    I am using a property list for counting elements, which means that eq is implicitly used for detecting equivalent elements. If you want to use
    other comparison functions, it is better to use an association list
    (which makes the code a little bit wordier).

    In general, LOOP is a pretty good poor man's list comprehension
    facility. Just ignore that it performs iteration and use it to emulate a more declarative style.

    Gauche Scheme

    (apply values
    (map (cut map car <>)
    (values->list
    (partition (^(xs) (= 1 (cdr xs)))
    (rlet1 res ()
    (dolist (x '(a b c d e f g b d f))
    (if-let1 found (assoc x res)
    (inc! (cdr found)) (push! res `(,x . 1)))))))))

    ===>
    (g e c a)
    (f d b)


    --- Synchronet 3.21d-Linux NewsLink 1.2