• 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. how to remove repeat element in a list

Stats

  • Locked Locked
  • Replies 13
  • Subscribers 145
  • Views 23200
  • 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

how to remove repeat element in a list

tomchen
tomchen over 12 years ago

Hi 

I want to remove repeated element that in a list, but I can't find the related function for this.

I try to use hash table to solve it , but the order of this list will be changed

does any other function for this ??

ex:

                                                      change to 

aa= list("AA" "AA" "AA" "BB" "BB")  -----------------> aa = list("AA" "BB)

 

  • Cancel
Parents
  • Andrew Beckett
    Andrew Beckett over 11 years ago

    Jim,

    The downside of this implementation is that it is O(n^2) and whilst the list is in order, it's in the order of the last occurrence rather than first occurrence of a repeated value.

    An ordered version which is a bit cleaner than Derek's and avoids a reverse is:

     procedure(CCFuniqueInOrder(lst)
      let(((uniq makeTable('uniq)))
        setof(elem lst
        !boundp(uniq[elem]) && (uniq[elem]=t)
        )
      )
    )

    This is making the assumption that 'unbound is not in the list (which is probably a reasonable assumption in most scenarios).

    For a list with 1000000 random integers with values up to 10000,  this took 0.65s (on my laptop running on battery only), whereas I gave up with yours. Running with a list 10 times shorter took 29s, so I'd reasonably expect the original list to take 100 times longer than that, i.e. ~2900s. My example is roughly O(n).

    Regards,

    Andrew.

    • Cancel
    • Vote Up +1 Vote Down
    • Cancel
Reply
  • Andrew Beckett
    Andrew Beckett over 11 years ago

    Jim,

    The downside of this implementation is that it is O(n^2) and whilst the list is in order, it's in the order of the last occurrence rather than first occurrence of a repeated value.

    An ordered version which is a bit cleaner than Derek's and avoids a reverse is:

     procedure(CCFuniqueInOrder(lst)
      let(((uniq makeTable('uniq)))
        setof(elem lst
        !boundp(uniq[elem]) && (uniq[elem]=t)
        )
      )
    )

    This is making the assumption that 'unbound is not in the list (which is probably a reasonable assumption in most scenarios).

    For a list with 1000000 random integers with values up to 10000,  this took 0.65s (on my laptop running on battery only), whereas I gave up with yours. Running with a list 10 times shorter took 29s, so I'd reasonably expect the original list to take 100 times longer than that, i.e. ~2900s. My example is roughly O(n).

    Regards,

    Andrew.

    • Cancel
    • Vote Up +1 Vote Down
    • Cancel
Children
  • Dushyant
    Dushyant over 6 years ago in reply to Andrew Beckett

    Hi Andrew,

    I managed to cook up a faster implementation using tables, based on a solution elsewhere:

    (defun rmDuplicates (lst "l")

      (let ((r (makeTable 'r)))

        (foreach e lst r[e]=t)

        r->?))

    Sorting an array of symbols 10000000 times, I see that my function is about 1.4x faster.

    Maybe tables are the way to go for efficient skill programs!

    Dushyant

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 6 years ago in reply to Dushyant

    Hi Dushyant,

    Yes, you'll find that implementation in an older post I made 7 years ago (I'm sure I've posted it elsewhere before that too). The implementation above specifically was to allow the resulting list to be in the same order as in the input list, whereas the approach you've shown and in my other post will list the values in an arbitrary order. If the order doesn't matter, then the ->? approach would be fine. However, the saving is relatively small since they're both O(N) rather than O(N^2) and in most practical implementations the difference would probably be dominated by something else in the overall application where this is used, I'd expect. Both use hash tables to gain performance here - they can be useful for this purpose - although are not always the saviour for efficiency as it depends on what you're doing...

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Dushyant
    Dushyant over 6 years ago in reply to Andrew Beckett

    Hi Andrew,

    Ok -- I got so deep into speeding it up that I did not catch the ordering requirement in the question. Stuck out tongue

    Thanks for these solutions, though. They are really helpful!

    On a different note -- do you know if there are the likes of common lisp's functional programming features like reduce or Haskell's foldl, foldr? They are pretty useful for lisp processing, but I was not able to find them in SKILL.

    Dushyant

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 6 years ago in reply to Dushyant

    There's a discussion (and example of foldr) in this thread. I haven't seen any formal requests for Common Lisp's reduce function to be implemented in SKILL.

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

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