## Sorting Without Duplicates

### February 7, 2017

My first thought was to build up a frequency table of the items in the array, perhaps in an AVL tree, then scan the tree to produce the desired output. But that doesn’t meet the requirement to perform the work in-place, and it won’t have O(*n*) time complexity.

Thus, my suggestion is to sort the array by any desired method, using some kind of counting sort or radix sort to get O(*n*) time complexity, or any O(*n* log *n*) sorting algorithm if you don’t care about that, then scan the array, moving duplicates to the end. When the first portion of the array is sorted without duplicates, reverse the remainder of the array and process that part of the array recursively, stopping when there is no sub-array left to process. That’s a sort followed by a scan, so it’s linear if the sort is linear. The trick is the “moving duplicates to the end” part, which can easily become non-linear. It works better with lists, which even if they’re not in-place at least don’t require more storage than the original. Function `partition`

scans a sorted input to find the first partition, returning it along with the sorted remainder; function `sort-dups`

handles one partition after another, appending them at the end:

(define (partition lt? xs) ; assumes sorted xs (let loop ((xs xs) (prev #f) (ys (list)) (zs (list))) (cond ((null? xs) (values (reverse ys) (reverse zs))) ((equal? (car xs) prev) (loop (cdr xs) prev ys (cons (car xs) zs))) (else (loop (cdr xs) (car xs) (cons (car xs) ys) zs)))))

(define (sort-dups lt? xs) (let loop ((xs xs) (zs (list))) (if (null? xs) (apply append (reverse zs)) (call-with-values (lambda () (partition lt? xs)) (lambda (first rest) (loop rest (cons first zs)))))))

And here is the function at work on some sample data:

> (sort-dups < (sort < '(2 9 1 5 1 4 9 7 2 1 4))) (1 2 4 5 7 9 1 2 4 9 1) > (sort-dups < (sort < '(1 1 1 1 1 1 1 1 1))) (1 1 1 1 1 1 1 1 1) > (sort-dups < (sort < '())) () > (sort-dups < (sort < '(7 3 1 6 2 5 4))) (1 2 3 4 5 6 7)

Time complexity of `sort-dups`

is O(*n*^{2}) in the worst case, where all the integers are identical, but O(*n* log *n*) in the average case. I don’t see any way to reduce that, but maybe one of my smart readers has a better idea. I thought of using a hash table to collect the frequencies, but you still have to sort the keys, which makes the problem O(*n* log *n*), and you still have to deal with the case of identical integers.

You can run the program at http://ideone.com/j0tZR8.

Pages: 1 2

In Python. This solution is not in-place.

In Python. I believe it is O(n) (as Python counter dictionary takes n inserts) and the second for loop has exactly n elements.

@Rutger: You have to sort ctr in line 9, as the order is otherwise not defined. Try for example to multiply all entries of example with 101 and see what happens. (I get then [707, 404, 101, 505, 202, 909, 404, 101, 202, 909, 101])

A sort (could be in-place) followed by a reordering that is much like mergesort (but requires not only ordered input but a disjoint split, because I was not able to deal with ties correctly when combining the parts). It might be O(n log n), even in the worst case, and it might not be impossible to do in-place or with less space than here. The combination step attempts to pick a large element whenever possible; it seemed to work for many examples after I disallowed ties. This also is in Python.

I don’t think an O(n) solution is possible. If it was, you could use it to construct an O(n) sort of any array without ties, and apply it to arrays with ties by, for example, using the element’s original index to break them.

Anyway, because of the way its “transpose” function behaves, the Haskell solution is almost embarrassing: we can sort the array, group the elements, use transpose to turn it into a list of the correct subarrays, and concatenate the whole thing together:

Inspired by@Kevin another Python solution.

Here’s a Haskell version in the REPL. It’s not in-place.

[…] the comments to the previous exercise, reader Kevin gave a beautiful solution in Haskell; it sorts the input items, groups them into […]

Uses ‘key’ function of the built in ‘list.sort()’ method to sort the list into the desired order, in-place.

The trick or insight is to generate a key for each item in the list that reflects the number of times that item has been seen as well as the item itself. For example, in the test sequence below the three 1’s would get keys: (0,1), (1,1), (2,1).

The dd is a ‘defaultdict’ that holds an ‘itertools.count()’ counter for each unique value in the list. The key function takes a list item and returns a tuple (next value of the counter associated with that item, item).

Complexity is O(n log n). (The key function is called once for each element in the list, O(n). Sorting is O(n log n)).