## Remove Duplicates From A List

### December 17, 2013

We have today another exercise from our infinite supply of interview questions:

Return a list containing only one copy of any duplicates in an input list, with items in the output in the same order as their first appearance in the input.

Your task is to answer the interview question given above; you must provide at least two different solutions. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisements

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