How to get rid of duplicates in a list, but keep the order

前端 未结 8 1283
情话喂你
情话喂你 2021-02-15 15:13

I am using Intermediate Student with Lambda in DrRacket, I was wondering how one would remove the duplicates in a list, while keeping the order. For example (remo

相关标签:
8条回答
  • 2021-02-15 16:15

    Old question, but this is an implementation of J-Y's idea.

    (define (dup-rem lst)
      (cond
        [(empty? lst) empty]
        [else (cons (first lst) (dup-rem (filter (lambda (x) (not (equal? (first lst) x))) lst)))]))
    
    0 讨论(0)
  • 2021-02-15 16:15

    I'm not sure if this is homework, but in case it is I'll post just the idea. If it's not tell me and I can put a solution here.

    What you need is to keep track of the unique items you find, you can do that by using an auxiliary list, like an accumulator, to keep track of the ones you found so far.

    Whenever you look at another item check to see if it's in the auxiliary list. In case it's not add it to the auxiliary list.

    You'll end up with a reverse order of what you're trying to find, so you can just (reverse ...) it and you'll have your answer.

    0 讨论(0)
提交回复
热议问题