Removing duplicates from a list while still maintaining order in Dr. Racket.

73 views
Skip to first unread message

JJ C

unread,
Jul 20, 2020, 12:04:45 PM7/20/20
to Racket Users

In Beginning Student with List Abbreviations

I am struggling to come up with functions to remove duplicates from a list while maintaining the order of the list.

one function to remove duplicates from the left,

i.e. 1 2 1 3 2 4 5 -> 1 2 3 4 5

and one from the right.

i.e. 1 2 1 3 2 4 5 -> 1 3 2 4 5

What are the functions for removing duplicates from each left and right side? If you need to, use helper functions and append, cond, cons, equal?, etc but not using reverse or any built-in functions.

David Storrs

unread,
Jul 20, 2020, 1:09:39 PM7/20/20
to JJ C, Racket Users
First off, if this was an intentional dinner post, well played. :>


If you don't want to use built-in functions, I would look at using a hash to track what you've already seen and then cons things together depending on if they're in the hash.

For the second, maybe recur through the list using a hash to track the most recent index of each item, then pop back up through the recursion consing things together based on whether you're at the specified index. That's my first thought, anyway.


--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/6049507b-094e-43c9-9dda-28237fcb57d8n%40googlegroups.com.

David Storrs

unread,
Jul 20, 2020, 1:10:59 PM7/20/20
to JJ C, Racket Users
"double post", not "dinner post".  Damn. Email needs an edit function and no autocorrect.

Prabhakar Ragde

unread,
Jul 21, 2020, 8:52:48 AM7/21/20
to Racket Users
Could you point us to the original homework question so we can be sure of the requirements? Thanks.

Averell Aquino

unread,
Jul 25, 2020, 4:36:14 AM7/25/20
to Racket Users
I see that you asked regarding this in two other posts:
https://groups.google.com/g/racket-users/c/TydSKVT5cqo (On which I provided a function for removing duplicates from the right)

You just have to follow the design recipe for beginning student languages and you will get these for removing duplicates from the left:

; List<Any> -> List<Any>
; returns a list which has only the left most occurrences of the elements of lst
(check-expect (unique-left '()) '())
(check-expect (unique-left (list "a")) (list "a"))
(check-expect (unique-left (list "a" "b" "a")) (list "a" "b"))
(check-expect (unique-left (list "a" "b" "a" "c" "d" "c")) (list "a" "b" "c" "d"))
(define (unique-left lst)
  (cond
    [(empty? lst) '()]
    [else (if (mymember? (first lst) (unique-left (rest lst)))
              (cons (first lst) (remove-member (first lst) (unique-left (rest lst))))
              (cons (first lst) (unique-left (rest lst))))]))

; remove-member
; Any List<Any> -> List<Any>
; remove first occurrence of elt in lst
(check-expect (remove-member "a" '()) '())
(check-expect (remove-member "a" (list "a")) '())
(check-expect (remove-member "a" (list "a" "b" "a")) (list "b" "a"))
(check-expect (remove-member "c" (list "a" "b" "a")) (list "a" "b" "a"))
(define (remove-member elt lst)
  (cond
    [(empty? lst) '()]
    [else (if (equal? (first lst) elt)
              (rest lst)
              (cons (first lst) (remove-member elt (rest lst))))]))


If it is allowed to design your own reverse function then this will also work:

(define (unique-left lst)
  (reverse (unique-right (reverse lst))))
Reply all
Reply to author
Forward
0 new messages