• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Remove duplicates from a list

Stats

  • Locked Locked
  • Replies 9
  • Subscribers 145
  • Views 20799
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Remove duplicates from a list

tweeks
tweeks over 13 years ago

Just wanted to share my solution to this common problem.

(defun nub (l "l")
  (let ((table (makeTable "" nil)))
    (foreach e l table[e] = t)
    table->?))

Output:

(nub (parseString "Banana Rama" "")) => ("a" "m" "n" "R" "B" " ")

This assumes order doesn't matter. If order does matter (and efficiency doesn't...), you could use this O(n^2) version, which is a literal translation of the one in the Haskell Data.List library:

(defun nub (l "l")
  (defun _nub (xs ls "ll")
    (unless (null xs)
      (destructuringBind (x @rest xs) xs
        (if (member x ls)
            (_nub xs ls)
          (cons x (_nub xs (cons x ls)))))))
  (_nub l nil))

Output:

(nub (parseString "Banana Rama" "")) => ("B" "a" "n" " " "R" "m")

If you want to preserve order and get O(n) performance, you could modify the last version to use a table instead of a list:

(defun nub (l "l")
  (defun _nub (xs ls "lo")
    (unless (null xs)
      (destructuringBind (x @rest xs) xs
        (if (ls[x])
            (_nub xs ls)
          (cons x (_nub xs (ls[x] = t && ls)))))))
  (_nub l (makeTable "" nil)))

Output:

(nub (parseString "Banana Rama" "")) => ("B" "a" "n" " " "R" "m")
  • Cancel
Parents
  • pcbnagaraj
    pcbnagaraj over 12 years ago

     It worked!!.

    Thanks,

    Nagaraj.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • pcbnagaraj
    pcbnagaraj over 12 years ago

     It worked!!.

    Thanks,

    Nagaraj.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
No Data

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information