Page 7: Removing Duplicates
Unit 5, Lab 1, Page 7
On this page, you will learn a recursive technique to remove duplicates from a list.
Suppose you have a list of items and want to know if the elements of the list are distinct. For example, you might want to make sure that you don’t have anything on your shopping list twice.
As a first step, we’ll just answer the yes/no question: are there any duplicates on your list?
Experiment with the block using a few different input lists. What does this block report?
Finish building the
are the items distinct?
predicate, which is started below. Since you’re writing a predicate, your procedure should always reporttrue
orfalse
.Try doing it without this hint.
If the program gets to the third
report
block at the bottom, what does that tell you about the items in the list?
Notice that this procedure calls itself at the end. (It is recursive.) This won’t work if the input to that call is the same as the original input. So you can’t say . Instead, you have to use a smaller input value.
If you doubled the length of the list, would this algorithm take the same amount of time? Twice as long? More than twice as long?
For your grocery list, you wouldn’t just want to know whether or not there are duplicates. You’d want a new list with the duplicates removed.
Experiment with the block using a few different inputs. What does this block report?
Build a
distinct items from
reporter using a structure similar to that of theare the items distinct?
predicate.The algorithm for this block will make the same decisions as in
are the items distinct?
. But that was a predicate. This one has to report a list. So look at the threereport
blocks in your code for theare the items distinct?
predicate, and decide what they should report for thedistinct items from
reporter.If the list is empty, what should
distinct items from
report?If the first item in the list appears in the rest of the list, it doesn’t matter which copy you leave out. Is there an easy way to get a version of the list without one of those copies?
- What if there are other duplicates in that list?
If computer makes it to the third
report
block, what does that tell you about the first item in the list? Do you want the first item as part of the list you report?
Test it. Be sure to pick good test cases: More than one pair of duplicates, more than two items of the same value, duplicates right next to each other in the list, etc.
“U5L1-Removing-Duplicates”