Hostname: page-component-8448b6f56d-m8qmq Total loading time: 0 Render date: 2024-04-23T12:12:40.914Z Has data issue: false hasContentIssue false

Functional Pearls: On removing duplicates

Published online by Cambridge University Press:  10 August 2016

Richard S. Bird*
Affiliation:
Programming Research Group, Oxford University, UK
Rights & Permissions [Opens in a new window]

Extract

Core share and HTML view are not available for this content. However, as you have access to this content, a full PDF is available via the ‘Save PDF’ action button.

The function remdup (also called mkset in some functional languages) removes duplicates from a given list. The following definition of remdup is standard and leads to a quadratic time algorithm

The operator (—) used in the last expression subtracts one list from another; its definition is

Defined in this way remdup x returns the list of distinct elements of x in the order in which they first appear in x. In other words, the position of remdup JC as a subsequence of x is lexically the smallest among all possible solutions.

Now let us change the problem and ask that remdup simply return the lexically least solution. Note the subtle difference between this version and the previous one: before, it was the position of the subsequence that was lexically the least, now it is the subsequence itself. To make the distinction clear, consider x = [1,4,2,4,3]. With the original definition remdup x returns [1,4,2,3]; the lexically least subsequence, however, is [1,2,4,3].

The question we are interested in is this: can we also find a quadratic time algorithm for the new problem? The answer turns out to be yes, but justifying it requires a bit of work.

Type
Research Article
Copyright
Copyright © Cambridge University Press 1991

References

Bird, R. S., 1987. An introduction to the theory of lists. In: Broy, M. (editor), Logic of Programming and Calculi of Discrete Design, NATO Series F, vol 36. Springer-Verlag.Google Scholar
Bird, R. S., 1989. Lectures on constructive functional programming. In: Broy, M. (editor), Constructive Methods in Computing Science, NATO Series F, vol 52. Springer-Verlag.Google Scholar
Submit a response

Discussions

No Discussions have been published for this article.