# Non Unique Elements

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.

• Related Oct 13, 2015 at 10:11
• since this is for array of integers code would be different. I think much shorter. That is for a string. Oct 13, 2015 at 10:21
• 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"? Oct 13, 2015 at 15:01
• Does the output have to be a list or would a set be acceptable? Oct 13, 2015 at 15:06
• And does it have to output in that format? Oct 13, 2015 at 15:06

# 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


# 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

# 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]


## 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


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.

# J, 1214 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@=


# Jelly, 4 bytes, language postdates challenge

œ-QQ


Try it online!

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

• This look so cryptic 0.o what is that first character? May 10, 2017 at 2:54

# PHP, 75 bytes

function a($b){return array_unique(array_diff_assoc($b,array_unique($b)));}  Try it online! ## Python 2.7, 36 bytes list({x for x in i if i.count(x)>1})  Where i is the input • You'll need to make it a lambda function because snippets aren't allowed. Jun 6, 2018 at 17:45 • 3 other people did the same thing, and OP doesn't seem to mind Jun 6, 2018 at 18:08 • "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. Jun 6, 2018 at 18:39 # 05AB1E, 86 5 bytes ʒ¢≠}Ù  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]  # Kotlin, 41 bytes {it.filter{v->it.count{it==v}>1}.toSet()}  Try it online! # Perl 6, 14 bytes *.repeated.Set  Try it online! Gets a Set of the repeated elements. # Husk, 4 bytes u-u¹  Try it online! u # unique element of - # set difference between u¹ # unique elements of input # and input  # JavaScript (Node.js), 60 bytes f=>[...new Set(f.filter(e=>f.indexOf(e)!=f.lastIndexOf(e)))]  Try it online! # JavaScript (Node.js), 43 bytes (another try) a=>a.filter((t={},e=>!(1-(t[e]=++t[e]|0))))  Try it online! # Burlesque, 9 bytes peJNB\\NB  Try it online! pe # Parse and push J # Duplicate NB # Remove duplicate elements \\ # Difference between nubbed and original lists NB # Nub the difference  # Perl 5, 18 bytes Since a snippet is allowed here, the input is assumed to be in @F. The TIO link adds a little I/O around it. grep++$k{$_}==2,@F  Try it online! # R, 24 bytes names((t=table(v))[t>1])  Try it online! Completely different approach to the previous R answer for the same number of bytes when written (as here) as a snippet with pre-defined variable v, but 4 bytes shorter as a full program (which, admittedly, isn't required by the challenge). Outputs the non-unique elements as a vector of strings. # R, 25 bytes (r=rle(sort(v)))$v[r\$l>1]


Try it online!

One byte longer (and yet another approach) if you want to output as an integer vector.