-2
\$\begingroup\$

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!
\$\endgroup\$
3
  • 4
    \$\begingroup\$ Please state what you mean by a "list", and how the program will interact with this list (i.e. input/output) \$\endgroup\$
    – user12205
    Apr 17, 2014 at 12:53
  • 4
    \$\begingroup\$ I 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 \$\endgroup\$
    – Jo King
    Dec 20, 2019 at 3:53
  • 1
    \$\begingroup\$ @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 \$\endgroup\$
    – Quintec
    Dec 20, 2019 at 23:06

7 Answers 7

4
\$\begingroup\$

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:

∪⎕
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 17, 2014 at 13:01
3
\$\begingroup\$

GolfScript (2 bytes)

.&

Takes input on stack, leaves output on stack.

\$\endgroup\$
1
  • \$\begingroup\$ GolfScript beats all!as usual... \$\endgroup\$
    – Quintec
    Apr 17, 2014 at 14:23
1
\$\begingroup\$

Red, 8 bytes

unique s

assuming list is in word 's

\$\endgroup\$
0
\$\begingroup\$

Python 2: 12 bytes

print set(l)

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

\$\endgroup\$
0
\$\begingroup\$

Python - 12 bytes

list(set(l))

Assuming the list is given in the variable l.

\$\endgroup\$
0
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.