24
\$\begingroup\$

Write a program which finds the non-unique elements of an array of signed integers. The resulting array can be in any order.

Your answer may be a snippet which assumes the input to be stored in a variable (d, say) and evaluates to the correct result.

Test Cases

Each test case is a single line in the format input => output. Note that other permutations of the output are valid as well.

[]                        => []
[-1, 0, 1]                => []
[1, 1]                    => [1]
[3, 0, 0, 1, 1, 0, 5, 3]  => [3, 0, 1]
[-34, 0, 1, -34, 4, 8, 4] => [-34, 4]

Order of the elements doesn't matter.

This is code golf, so the shortest answer (in bytes) wins.

\$\endgroup\$
  • 4
    \$\begingroup\$ Related \$\endgroup\$ – Sp3000 Oct 13 '15 at 10:11
  • 1
    \$\begingroup\$ since this is for array of integers code would be different. I think much shorter. That is for a string. \$\endgroup\$ – garg10may Oct 13 '15 at 10:21
  • 1
    \$\begingroup\$ Are we allowed to accept input as lines instead of as an array? For example, instead of [-1, 0, 1], can we input (replace \n with newlines): "-1\n0\n1"? \$\endgroup\$ – Addison Crump Oct 13 '15 at 15:01
  • 1
    \$\begingroup\$ Does the output have to be a list or would a set be acceptable? \$\endgroup\$ – Dennis Oct 13 '15 at 15:06
  • \$\begingroup\$ And does it have to output in that format? \$\endgroup\$ – Addison Crump Oct 13 '15 at 15:06

44 Answers 44

2
\$\begingroup\$

Japt, 7 bytes

â £kX â

Try it online!

Explanation:

â £kX â
â         // Get all unique items from the input
  £       // Map X through the results
   kX     //   Remove X from the input
      â   //   Get unique items
-h        // Return the last item
\$\endgroup\$
1
\$\begingroup\$

Hassium, 104 Bytes

func main(){r=[];i=[];foreach(e in d)if(!i.contains(e))i.add(e)else if(!r.contains(e))r.add(e);print(r)}

Run online and see expanded here

\$\endgroup\$
1
\$\begingroup\$

Python, 143 bytes

There are already some very good Python answers out there (see @dieters, @ppperry & @xnor answers) so I decided to take a different approach, have some fun with recursion and python lambda functions and see what could I come up with. This is not even close to the best answers here but it was fun to think.

The program takes the integer list as a parameter of function p and returns a list containing non-unique elements.

p=lambda i:[j for j in(lambda n:[]if len(n)<=1 else[(lambda x:x[0]if len([1 for o in x[1:]if x[0]==o])==1 else 'N')(n)]+p(n[1:]))(i) if j!='N']

Two line version for better readability:

p=lambda i:[j for j in(lambda n:[]if len(n)<=1 else[(lambda x:x[0]if 
len([1 for o in x[1:]if x[0]==o])==1 else 'N')(n)]+p(n[1:]))(i) if j!='N']

Short Explanation

The inner lambda takes a list x as a parameter and returns the first element of such list if it is repeated only once in the rest of the list (x[1:]). If not it returns 'N'.

The lambda in between the outer and inner lambdas is the one in charge of the recursive search. This returns a list containing the non-unique elements and plenty of 'N's. This is the one which takes the integer list as a parameter.

The outer lambda filters the resulting list getting rid of the unwanted 'N's.

Test cases

p([])
[]
p([-1,0,1])
[]
p([3, 0, 0, 1, 1, 0, 5, 3])
[3,0,1]
\$\endgroup\$
1
\$\begingroup\$

STATA, 18 bytes

bys v:drop if _n-2

Note that this requires the paid version of STATA.

Assumes the data is stored in a variable v and then the result will be in v at the end. It works by sorting by v and grouping duplicate values together. Each duplicate value will have at least two values, which means it can drop anything that is not the second element of each value. Third and subsequent elements will already be represented and the first element will either be unique or represented by the second.

For example, make a file a.b with data:

3
0
0
1
1
0
5
3

Then run the following code

insheet using a.b,clear
rename v1 v
bys v:drop if _n-2 //the actual code that does stuff
list v,noobs noheader
\$\endgroup\$
1
\$\begingroup\$

C# - 40

i.GroupBy(g=>g).SelectMany(s=>s.Skip(1))

Using select many to be able to use Skip and skip the first occurrence if it exists.

\$\endgroup\$
1
\$\begingroup\$

J, 12 14 13 bytes

~.d#~1<+/=/~d

Assumes the list is in d.

Explanation:

         =/~d   NB. compare each element in d to each element in d
       +/       NB. sum the columns (giving amount of occurrences)
     1<         NB. see which columns are greater than 1 (=not unique)
  d#~           NB. select those elements from d 
~.              NB. unique elements from that

If an anonymous function is OK as well, it can be shortened to 13 12:

~.#~1<+/"1@=
\$\endgroup\$
1
\$\begingroup\$

Jelly, 4 bytes, language postdates challenge

œ-QQ

Try it online!

Same algorithm as the accepted answer, just a shorter syntax.

\$\endgroup\$
  • \$\begingroup\$ This look so cryptic 0.o what is that first character? \$\endgroup\$ – garg10may May 10 '17 at 2:54
1
\$\begingroup\$

PHP, 75 bytes

function a($b){return array_unique(array_diff_assoc($b,array_unique($b)));}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 2.7, 36 bytes

list({x for x in i if i.count(x)>1})

Where i is the input

\$\endgroup\$
  • \$\begingroup\$ You'll need to make it a lambda function because snippets aren't allowed. \$\endgroup\$ – Nissa Jun 6 '18 at 17:45
  • \$\begingroup\$ 3 other people did the same thing, and OP doesn't seem to mind \$\endgroup\$ – Khalil Jun 6 '18 at 18:08
  • 5
    \$\begingroup\$ "Your answer may be a snippet which assumes the input to be stored in a variable". Note that this is not the default, but this question is old and the OP explicitly allowed it, so it is okay. \$\endgroup\$ – mbomb007 Jun 6 '18 at 18:39
1
\$\begingroup\$

05AB1E, 8 6 5 bytes

ʒ¢≠}Ù

Try it online or verify all test cases.

Explanation:

ʒ  }     # Filter the list by:
 ¢       #  Where the count of the current item in the input-list
         #   i.e. [1,2,3,2,1,1,6,4,5,6] and 1 → 3
         #   i.e. [1,2,3,2,1,1,6,4,5,6] and 5 → 1
  ≠      #  is not 1
         #   i.e. 3 → 1 (truthy)
         #   i.e. 1 → 0 (falsey)
    Ù    # After filtering, uniquify the remaining items
         #  i.e. [1,2,2,1,1,6,6] → [1,2,6]
\$\endgroup\$
1
\$\begingroup\$

Kotlin, 41 bytes

{it.filter{v->it.count{it==v}>1}.toSet()}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 6, 14 bytes

*.repeated.Set

Try it online!

Gets a Set of the repeated elements.

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

JavaScript (Node.js), 60 bytes

f=>[...new Set(f.filter(e=>f.indexOf(e)!=f.lastIndexOf(e)))]

Try it online!

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

JavaScript (Node.js), 43 bytes (another try)

a=>a.filter((t={},e=>!(1-(t[e]=++t[e]|0))))

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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