Remove Duplicates From One List

-2

Task:

Remove all duplicates from one list of integers. A list is simply a sequence of connected values that allows the same values to be stored at different positions in this sequence.

If an item was found to be the same value as another item in this list, keep the first occurence of the item and remove the second (third and so on...) occurence of the item.

Input/Output

You may write a function or full program that takes a list of integers as an input and returns a list without duplicates as an output.

Rules:

  • This is a challenge, so shortest answer wins!

Quintec

Posted 2014-04-17T12:23:20.750

Reputation: 2 801

4I would have preferred leaving this question closed and creating a new one, since it is old and the edit invalidates almost all of the existing answers – Jo King – 2019-12-20T03:53:36.833

1@JoKing same here. I have no idea why/who reopened, but I thought it was best to clean it up if it was being reopened – Quintec – 2019-12-20T23:06:27.213

4Please state what you mean by a "list", and how the program will interact with this list (i.e. input/output) – user12205 – 2014-04-17T12:53:20.243

Answers

4

APL (1)

takes a list and returns the unique elements in a list, i.e.:

    ∪ 1 2 3 2 3 4 5
1 2 3 4 5

if you need I/O it's 2:

∪⎕

marinus

Posted 2014-04-17T12:23:20.750

Reputation: 30 224

Since it's not specified, default counting for code-golf is in bytes (according to the tag wiki), which would make 2 (UTF-16) or 3 (UTF-8) bytes. Together with I/O, that's 4 or 6. – Martin Ender – 2014-04-17T13:01:09.847

3

GolfScript (2 bytes)

.&

Takes input on stack, leaves output on stack.

Peter Taylor

Posted 2014-04-17T12:23:20.750

Reputation: 41 901

GolfScript beats all!as usual... – Quintec – 2014-04-17T14:23:30.543

1

Red, 8 bytes

unique s

assuming list is in word 's

AnnoyingUtensil

Posted 2014-04-17T12:23:20.750

Reputation: 11

0

Python 2: 12 bytes

print set(l)

No input spec, so I'm assuming the list is held in a variable called l.

undergroundmonorail

Posted 2014-04-17T12:23:20.750

Reputation: 5 897

0

Python - 12 bytes

list(set(l))

Assuming the list is given in the variable l.

Tal

Posted 2014-04-17T12:23:20.750

Reputation: 1 358

0

Mathematica, 7 bytes or 3 characters

Assumes input in variable x

Union@x

If we're counting in Mathematica characters, I can do 3 (the second input line):

enter image description here

Martin Ender

Posted 2014-04-17T12:23:20.750

Reputation: 184 808

0

Ruby, 3 characters

a|a

Assumes the list is stored in variable a. Also works with & instead of |.

If the original list has to be altered (instead of keeping the original the same and creating a new list with duplicates removed), then 4 characters:

a|=a

Doorknob

Posted 2014-04-17T12:23:20.750

Reputation: 68 138