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

I use Intermediate Student with Lambda in DrRacket, I was wondering how to remove duplicates in the list, while maintaining order. For example, (remove-dup (list 2 5 4 5 1 2)) will create (list 2 5 4 1) . So far I have this:

 (define (remove-duplicates lst) (cond [(empty? lst) empty] [(member? (first lst) (rest lst)) (remove-duplicates (rest lst))] [else (cons (first lst) (remove-duplicates (rest lst)))])) 

but there is a problem, since it does not preserve order. Can someone point me in the right direction? Thank you for your time.

+6
source share
8 answers

If your goal is to get the functionality to work, and not some kind of homework question, you don't need to do anything, just use remove-duplicates :

 Welcome to Racket v5.2. -> (remove-duplicates (list 2 5 4 5 1 2)) '(2 5 4 1) 
+11
source

This solution:

 (define (remove-duplicates lon) (foldr (lambda (xy) (cons x (filter (lambda (z) (not (= xz))) y))) empty lon)) 
+4
source

SRFI-1 has delete-duplicates , although it is inefficient. (I'm not very familiar with Racket, but certainly it has SRFI-1 and source ...)

http://srfi.schemers.org/srfi-1/srfi-1.html#delete-duplicates

0
source

Scroll through the list by inserting each item into a hash table or other dictionary. If you try to insert an item that is already in the hash table, do not add it to the outgoing list.

0
source

What you need to do is compare in reverse order all the time. You can use the reverse function, which returns the list in reverse order. This way you always delete the second + element, not the first. Here is an example, however, it uses let and if expressions, not a cond expression.

http://www.cs.bgu.ac.il/~elhadad/scheme/duplicates.html

Good luck with your homework :)

0
source

I'm not sure if this is homework, but in case I post this idea. If that doesn't tell me, and I can find a solution here.

What you need is to track the unique items that you find, you can do this using an auxiliary list, such as a battery, to keep track of those that have been found so far.

Whenever you look at another item, check it in the auxiliary list. If he does not add it to the auxiliary list.

As a result, you get the reverse order of what you are trying to find, so you can just (change ...) it and you will have your answer.

0
source

Hmm, I recently had a racket exam: /

"standard" remove-duplicates works fine, but I used a rather large drRacket file, so I had to download it with (require racket/list)

here is an alternative way :)

using a mutation (not really in the spirit of a racket, but .. it works.)

  (define (set l) (define the-set '()) (begin (for-each (lambda (x) (if (member x the-set) #t (set! the-set (cons x the-set)))) l) (reverse the-set))) 

Hope this helps ... cheers!

0
source

An old question, but this is an implementation of the idea of ​​JY.

 (define (dup-rem lst) (cond [(empty? lst) empty] [else (cons (first lst) (dup-rem (filter (lambda (x) (not (equal? (first lst) x))) lst)))])) 
0
source

Source: https://habr.com/ru/post/901584/


All Articles