## Remove Duplicates From A List

### December 17, 2013

One version of the function traverses the input list, keeping a list of elements as they are seen, and adding each element to the result only if it has not been seen before:

`(define (rem-dups xs)`

(let loop ((xs xs) (seen (list)) (result (list)))

(cond ((null? xs) (reverse result))

((member (car xs) seen)

(loop (cdr xs) seen result))

(else (loop (cdr xs)

(cons (car xs) seen)

(cons (car xs) result))))))

`> (rem-dups '(a b a c a b a))`

(a b c)

If you are worried about the linear-time cost of looking up an element in the `seen`

list, you can use a hash table:

`(define (rem-dups xs)`

(let ((seen (make-hash)))

(let loop ((xs xs) (result (list)))

(cond ((null? xs) (reverse result))

((pair? (seen 'lookup (car xs)))

(loop (cdr xs) result))

(else (set! seen (seen 'insert (car xs) (car xs)))

(loop (cdr xs) (cons (car xs) result)))))))

`> (rem-dups '(a b a c a b a))`

(a b c)

Wherever there is a searching solution to a problem, there is also a sorting solution to the problem. Here we decorate each input item with its integer ordinal, sort by item and ordinal, scan the list keeping only the first appearance of each item, sort again by ordinal, and finally de-decorate the list (Perl programmers call this the “Schwartzian transform” and unix hackers will recognize the “sort | unique | sort” idiom):

`(define (rem-dups lt? xs)`

(map car

(sort (lambda (a b) (< (cdr a) (cdr b)))

(unique (lambda (a b)

(and (not (lt? (car a) (car b)))

(not (lt? (car b) (car a)))))

(sort (lambda (a b)

(cond ((lt? (car a) (car b)) #t)

((lt? (car b) (car a)) #f)

(else (< (cdr a) (cdr b)))))

(map cons xs (range (length xs))))))))

We use `range`

from the Standard Prelude, but the `unique`

in the Standard Prelude keeps the last of each set of adjacent duplicates, rather than the first, so we have to write our own `unique`

:

`(define (unique eql? xs)`

(if (or (null? xs) (null? (cdr xs))) xs

(let loop ((xs (cdr xs)) (zs (list (car xs))))

(cond ((null? xs) (reverse zs))

((eql? (car xs) (car zs)) (loop (cdr xs) zs))

(else (loop (cdr xs) (cons (car xs) zs)))))))

And here’s an example, complete with a function to compare symbols:

`> (rem-dups (lambda (a b)`

(stringstring a)

(symbol->string b)))

'(a b a c a b a))

(a b c)

You can run the program at http://programmingpraxis.codepad.org/5srWQ9rj.

Pages: 1 2

A solution in Haskell

import Data.List

uniqify = map head . group . sort

Haskell O(n logn) solution (library routine nub has O(n^2)). Using set to keep track of already seen elements:

It would have been better to start with empty map:

The shell command in full garb, with GNU utils: “cat -n | sort -k 2 -s | uniq -f 1 | sort -n | cut -f 2-” aka “number the lines, stable sort ignoring the numbers, uniq ignoring the numbers, sort by the numbers, drop the numbers” (still ignoring some subtleties that may happen when sort acts smart, depending on locale settings).

The first sort can also do the uniqification with “-u”.

For Python a discussion on this topic can be found at code.activestate.com.

def RemoveDupList(l):

cur = 0

dellist = []

for v in l:

for i in xrange(cur):

if l[i] == v:

dellist.append(cur)

break

cur = cur + 1

j = 0

for i in dellist:

del l[i – j]

j = j + 1

return l

l = [1,2,3,4,3,5,6,1,2,4,7,3,9,4,45,7,8,6,6]

print l

print RemoveDupList(l)

Python version, post again

def RemoveDupList(l):

cur = 0

dellist = []

for v in l:

for i in xrange(cur):

if l[i] == v:

dellist.append(cur)

break

cur = cur + 1

j = 0

for i in dellist:

del l[i – j]

j = j + 1

return l

l = [1,2,3,4,3,5,6,1,2,4,7,3,9,4,45,7,8,6,6]

print l

print RemoveDupList(l)

@zhujun: your method works fine, but it is awfully slow compared to one of the solutions named uniq presented in the link of my last post. If the input list is range(10000) * 3, then your solutions takes more than 9.5 seconds against 17 millseconds for uniq.

Assuming the list items are hashable, here are a few in Python:

2 simple solutions in common lisp

sorry, here they are.. ;)

(defun rm-duplicates1 (lst)

(defun rd-iter (lst newlist)

(cond ((eq lst nil) newlist)

((eq (member (car lst) newlist) nil)

(rd-iter (cdr lst) (append newlist (list (car lst)))))

(t (rd-iter (cdr lst) newlist))))

(rd-iter lst ‘()))

(defun rm-duplicates2 (lst)

(cond ((eq lst nil) ‘())

((eq (member (car lst) (cdr lst)) nil)

(cons (car lst) (rm-duplicates2 (cdr lst))))

(t (rm-duplicates2 (cdr lst)))))

Here’s another one in Haskell. I’m not too good with the fancy combinator stuff, so I’m sure there’s a slicker way, but I think I use scanl to good effect:

import Data.Set (Set)

import qualified Data.Set as Set

buildsets::Ord a => [a] -> [Set a]

buildsets = scanl (flip Set.insert) Set.empty

nub2::Ord a => [a] -> [a]

nub2 thelist = map fst $ filter (not . uncurry Set.member) (zip thelist (buildsets thelist))

A current first year comp sci student, my first post. I guess the key to this exercise is to use hashables.

import collections

from random import randrange

a = [randrange(0,10000) for x in xrange(0,100000)]

“””Two ways to remove duplicates from a list”””

def rmv_dup(a):

exists = set()

e_list = []

for i in a:

if i in exists:

continue

exists.add(i)

e_list.append(i)

return e_list

def rmv_dup2(a):

dup = collections.defaultdict(list)

dup_idx = []

for i,e in enumerate(a):

dup[e].append(i)

for k,v in dup.items():

if len(v) > 1:

for i in v[1:]:

dup_idx.append(i)

b = sorted(dup_idx)

for i in b[::-1]:

del a[i]

return a

A simple O(n) JS solution:

Python for you. Iterates through every number in the list, logging appearances in a dictionary. Appends the number to a duplicates list on the second appearance.

in c:

void removes()

{node *r;

r=head;

while(r!=NULL)

{

int m=r->val;

node *q=r;

node *p=r->next;

do

{if(p==NULL)

break;

if(m!=p->val)

{q=p;

p=p->next;

}

else

{q->next=p->next;

node *t=p;

free(t);

p=q->next;

display();}

}while(p!=NULL);

r=r->next;

}

}

In guile scheme.

PHP version:

$list = str_split(‘abddecbbafggiak’);

$deduped = getDedupedList($list);

print join(‘, ‘,$deduped);

function getDedupedList($array){

$newarray = array();

foreach($array as $k=>$v){

if(!in_array($v, $newarray)){

array_push($newarray, $v);

}

}

sort($newarray);

return $newarray;

}

SachinRaj (Beginer Java)

int i,j,k,rep=0,flag=0;

Scanner sc=new Scanner(System.in);

System.out.print(“Enter no of nos: “);

int no=sc.nextInt();

int[] org=new int[no];int recc[]=new int[no];

System.out.print(“Enter nos:”);

for(i=0;i<no;i++)org[i]=sc.nextInt();

for(i=0;i<no;i++)

{

for(j=0;j<i;j++)

{

if(org[i]==org[j])

rep++;

}

recc[i]=rep;

rep=0;

}

System.out.print("Array without duplicates: ");

for(i=0;i<no;i++)

{

if(recc[i]==0)System.out.print(org[i]+" ");

}

}

A one liner in scala(Folding over a tuple of Set and Empty list,returning the tuple with the current element appended to the list and the element added to the set if the current element is not in the Set, returning the tuple otherwise. Once the fold concludes, the reversed second element of the tuple is returned):

def listwithoutdupes[T](arr:List[T]):List[T] = arr.foldLeft((Set.empty[T],List.empty[T]))((a,b) => { if(a._1.contains(b)) a else (a._1 + b, b::a._2)})._2.reverse

Ruby novice here.

Trying to be clever, but I bet there is a simpler way…

def remove_dupes(ary)

h = {}

ary.keep_if { |x| !(h.has_key?(x) || h[x] = false) }

end

More clear:

def remove_dupes!(ary)

h = {}

ary.keep_if { |x| !( h[x] = h.key?(x) ) }

end

I can think of three different solutions.

1) The first solution, and the best one I can think of right now, is O(n) and uses the following objects:

– The input list (array)

– The output list (array)

– A visited list (hashtable)

– A duplicates list (hashtable)

In this implementation, the output list would be the sorted version of the duplicates hash, since hashes don’t ensure any particular order.

Code here: http://pastebin.com/WMEWGfVj

2) The second solution would be O(n*log(n)) and it would consist on sorting the input list first O(n*log(n)) and then loop through it once checking for matching consecutive elements.

So, I said three and posted two :P.

Further solutions are less efficient, of course. There’s an O(n^2) solution that loops the array for every item to find a dup.

Use java collections, create a LinkedHashSet and pass list in set constructo

this very simple in perl.

#!/usr/bin/env perl

use strict;

use warnings;

my %seen;

my @out = map {if ($seen{$_}) {()} else {$seen{$_} = 1;$_}} @ARGV;

print join(‘ ‘, @out), “\n”;

probably better as:

my @out = map {unless ($seen{$_}) {$seen{$_} = 1;$_}} @ARGV;

Hi Fahad. I think you misunderstood the problem. The original objective is to output the list of duplicates, whereas your code outputs the list “without” duplicates.

Now, just for fun, if the objective was to output the list without dups, then your first piece of code would be correct but the second one has a bug. The “unless” statement will return 1 (true) or 0 (false) for every item in the input list, so for every item that is a dup the unless statement will let a “1” pass through the map and into the output.

Cheers!

Juan Ignacio