# 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 '15 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 '15 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 '15 at 15:01
• Does the output have to be a list or would a set be acceptable? Oct 13 '15 at 15:06
• And does it have to output in that format? Oct 13 '15 at 15:06

# K5, 5 bytes

Assuming the input is already in a variable called d,

?d^?d


Take the distinct elements (?) of d except (d^) the distinct elements of d (?d). Nicely symmetrical, no? This works because the "except" operator removes only the first occurrence of the right argument from the left argument.

More generally,

nu: {?x^?x}


In action:

  nu'(();-1 0 1;1 1;3 0 0 1 1 0 5 3;-34 0 1 -34 4 8 4)
(()
()
,1
0 1 3
-34 4)


## Edit:

If we wanted to preserve the order of the first occurrence of non-unique elements, we could reverse the source list before and after we remove the unique elements via except at the cost of 4 extra bytes:

  nu: {?|(|x)^?x}
nu'(();-1 0 1;1 1;3 0 0 1 1 0 5 3;-34 0 1 -34 4 8 4)
(()
()
,1
3 0 1
-34 4)


# CJam, 10

Assuming the array is already in variable D (based on this comment):

D{De=(},_&


Try it online

Explanation:

D{…},   filter items of D based on the block
De=   count occurrences in D
(     decrement (resulting in true/false for duplicate/unique)
_&      remove duplicates from the results


Note: append a p if you want pretty printing, otherwise the resulting array is just printed out with no delimiters by default. That is acceptable since the question specifies the snippet only needs to "evaluate to the correct result".

## Standard input/output version, 13:

q~_{1$e=(},&p  Try it online Explanation: q~ read and evaluate the input array _ duplicate the array {…}, filter items based on the block 1$    copy the array
e=    count occurrences
(     decrement (resulting in true/false for duplicate/unique)
&       set intersection with the initial array (removes duplicates)
p       pretty print

• 13: q~$e{((<~}%p Oct 13 '15 at 10:29 • @Sp3000 I found another 13-byte version before reading your comment :) It preserves the order too. Oct 13 '15 at 10:30 ## Haskell - 32 import Data.List;f l=nub$l\\nub l


Pretty short, even with the import. a \\ b removes the first occurrence of each element of b from a, and nub makes all elements of a list unique.

# Pyth, 7 bytes

S{.-Q{Q


Try it online.

### How it works

Pyth automatically stores the evaluated input in Q and prints all unused return values.

     {Q  Convert Q into a set. This removes duplicates.
.-Q    Perform "bagwise" difference of Q and set(Q).
This removes the first occurrence of all elements in Q.
{       Convert to set to deduplicate.
S        Sort. Returns a list.


# SQL, 44 42 bytes

SELECT*FROM D GROUP BY I HAVING COUNT(*)>1


I hope it is OK to assume the integers are stored in table D? This will work in both SQLServer, PostgreSQL and possibly others. Thanks to @manatwork from the 2 bytes.

• Assuming i is the only field in table d, in PostgreSQL you can reduce it to select*from d group by 1having count(*)>1. (MySQL and SQLite's parser will also handle the unseparated select*from part, but they not understand 1having.) Oct 14 '15 at 10:04
• @manatwork cheers for that, sql server also understands the select*from. Doesn't like the 1having though.. will leave that as I having Oct 14 '15 at 18:25

## Mathematica, 29 26 bytes

Assuming that input is stored in d:

Select[d⋃d,d~Count~#>1&]


Otherwise, it's 29 bytes as an unnamed function:

Cases[#⋃#,n_/;#~Count~n>1]&


Here, d⋃d (or #⋃#) is a golfing trick to remove duplicates - by taking the set union with itself, Mathematica interprets the list as a set, removing duplicates automatically, while the actual union doesn't do anything.

Afterwards, both methods simply filter those elements which appear in the original list at least twice.

# JavaScript (ES6), 37 bytes

Run this in the JavaScript console:

e={};d.filter(x=>(e[x]=1+e[x]||0)==1)

• It's generally accepted that JavaScript needs some sort of explicit "output/print" function (such as console.log, alert, etc) to be considered complete. If a challenge says "write a program or function", then function returns also suffice. Aside that, very efficient solution! Oct 13 '15 at 14:08
• @Mwr247 The question states that the anwer may be a snippet which evaluates to the correct result. Oct 13 '15 at 14:09
• It seems I misinterpreted that paragraph. Apologies then =) Oct 13 '15 at 14:12
• @Mwr247 No problem! :) Oct 13 '15 at 14:13

# Matlab / Octave, 40

I'm assuming input values are real (not complex). The input is in a variable d.

unique(d(sum(triu(bsxfun(@eq,d,d')))>1))


Try it online in Octave.

• No need to input, you can assume data in variable 'd' Oct 13 '15 at 10:49
• @garg10may Thanks. Updated. You should specify that in your post Oct 13 '15 at 10:56
• The output is incorrect when d = [3, 0, 0, 1, 1, 0, 5, 3]. There are two 0s. Oct 13 '15 at 13:23
• @alephalpha Thanks! corrected (8 more bytes) Oct 13 '15 at 16:19
• Shorter: d(sum(triu(bsxfun(@eq,d,d')))==2). Or in Octave: d(sum(triu(d==d'))==2) Oct 14 '15 at 2:59

## Python 3.5, 30

[x for x in{*d}if~-d.count(x)]


Uses Python 3.5's set unpacking. The ~- subtracts 1, which takes a count of 1 to 0 which is Falsy.

This gives a list. If giving a set is OK, then we use a set comprehension, saving 1 char and not needing version 3.5:

{x for x in d if~-d.count(x)}

• SyntaxError: invalid syntax for Python 3 is it valid only for 3.5? When has python started becoming esoteric. Oct 14 '15 at 2:08
• @garg10may Just wait til you see what 3.6 has in store... Oct 14 '15 at 7:45
• @Sp3000 Awesome. Looks like the same set-up as Scala. Infinitely more readable than more alternatives. Oct 15 '15 at 18:19

# PowerShell, 31 29 Bytes

($d|group|?{$_.Count-1}).Name


Assumes that $d is already populated (as given) -- e.g., $d=@(-34,0,1,-34,4,8,4).

Pipes the array into the Group-Object cmdlet, which groups like-items together and spits out an object that's essentially an array of arrays. We pipe that to a Where-Object (the ? operator) that has Count greater than one (i.e., there are duplicates), and output the .Name of those items. Has a side bonus of preserving the initial ordering, too.

Edit - saved two bytes thanks to Danko Durbić

• I think you can replace $_.Count-gt1 with $_.Count-1 which would be true for any Count greater than one. Oct 13 '15 at 22:03
• @DankoDurbić Excellent! Oct 14 '15 at 12:39

# APL (Dyalog Unicode), 13 9 bytesSBCS

Anonymous tacit prefix function.

∊(⊂1↓⊣¨)⌸


Try it online!

()⌸ for each unique element (left argument) and the indices where it occurs (right argument), apply the following tacit function:

⊣¨ one of the left (the unique element) for each on the right (the indices)

1↓ drop one

⊂ enclose (prevents padding with zeros to create a non-ragged matrix)

∊ϵnlist (flatten)

# Julia, 30 29 bytes

∪(d[find(sum(d.==d',1)-1)])


d.==d' creates a symmetric matrix with the value at i,j being true if d[i]==d[j] and false otherwise. summing in one dimension and then subtracting 1 will produce zero if there's only one of the element and nonzero if there's more than one. find will obtain the indexes of the non-zero elements, which are then used to index the array d itself. ∪ (union) acts like unique when used in this way, removing the repeats.

Old solution:

∪(filter(i->sum(d.==i)>1,d))


Simple - for each entry, it checks if there's more than one of it in the array. Those for which there are more than one are returned by "filter", and then ∪ (union) acts like unique when used in this way, removing the repeats.

Note: originally had it as a function, but question allows array to be stored in a variable, for which I've chosen d as suggested in the question.

# Octave, 22 bytes

d(sum(triu(d==d'))==2)


# R, 31 24 bytes

Thanks to flodel for the 7 bytes.

Assuming the input is already in d.

### code:

unique(d[duplicated(d)])


edit: now it outputs correctly if there are more than 2 duplicates as pointed by aditsu.

• That looks beautiful! But the 4th test case doesn't seem to be correct... Oct 13 '15 at 14:51
• You can remove which since [ also accepts a logical argument. Oct 14 '15 at 4:31

# Mathematica, 31 29

Cases[{s_,t_/;t>1}:>s]@*Tally


# Pyth, 7 bytes

ft/QT{Q


Explanation:

ft/QT{Q
Q = eval(input())
{Q    set(Q) - deduplicate
f          filter - with T as the filter variable.
/QT      count in Q of T
t         minus 1.


The filter removes all elements that appear exactly once from the set of elements.

## LINQ,62 54 bytes

Kinda new here, but here goes nothing.

d.GroupBy(c=>c).Where(g=>g.Count()>1).Select(g=>g.Key)

• Welcome to the site! I don't know LINQ, but there's some whitespace you can probably remove from this to improve your score. Oct 14 '15 at 2:05

# Python 2.7, 36 42

list(set(filter(lambda x:d.count(x)>1,d)))


edit : surrounded the expression with list(..) in order to comply with the format required in the question

• this will output set not a list Oct 13 '15 at 12:49
• So shall I surround my snippet with a call to list(...) ? Oct 13 '15 at 13:33
• Yes the output should be an array only. Oct 14 '15 at 1:59

# K (not K5), 10 bytes

x@&1<#:'=x


Assumes input is in x. I thought it'd be fun to do a non-K5 answer!

• Assuming k4, & on a dictionary obviates the need for x@; it can also be made tacit as &1<#:'=: for 8 bytes (dropping the need for x to be initialized). As an aside, the order of the inputs is preserved either way. Dec 7 '20 at 19:12

# Python 3 - 33 30 bytes

{_ for _ in d if d.count(_)>1}

Repl output, d as input.

# K (oK), 7 bytes

Solution:

&1<#:'=


Try it online!

Explanation:

&1<#:'= / the solution
= / group, key => value (index)
#:'  / count length of each group
1<     / 1 less than
&       / keys where true


# Shell + GNU coreutils, 12

sort|uniq -d


### Test output:

$printf "%s\n" -34 0 1 -34 4 8 4 | ./nonuniq.sh -34 4$


## Mathematica, 23 bytes

With input stored in d:

Pick[#,#2>1]&@@@Tally@d


As a function, 24 bytes:

Pick[#,#2>1]&@@@Tally@#&


for example, with

d = {3, 0, 0, 1, 1, 0, 5, 3}
Tally@d


returns this:

   {{3, 2},
{0, 3},
{1, 2},
{5, 1}}


(first element of each sublist is the element, second one is frequency of occurrence). Applying to this list Pick[#,#2>1]&@@@ transforms it to

{Pick[3,2>1], Pick[0,3>1], Pick[1,2>1], Pick[5,1>1]}


And where the second argument of Pick evaluates to True the first argument is returned.

## Perl 6, 16 bytes

Assuming the list is stored in $_ you could use any of the following snippets. ( which was specifically allowed ) (--«.BagHash).Set.keys # 23 bytes  keys .Bag (-) .Set # 18 bytes  # U+2216 SET MINUS keys .Bag∖.Set # 16 bytes in utf8  If you don't care that you get a Bag you could leave off keys . $_ = [3, 0, 0, 1, 1, 0, 5, 3];
.Bag∖.Set ∋ 3 # True
.Bag∖.Set ∋ 5 # False


None of these have the limitation of only working on signed integers, or even just numbers for that matter.

say keys .Bag∖.Set given |(<a b c d a a c>), 1/3, 2/3 - 1/3;
# (a c 0.333333)


# JavaScript, 35 28 bytes

a=>a.filter(o=x=>!(o[x]^=1))


Try It Online!

After posting this, I realised that it was very similar to w0lf's solution.

# Ruby, 30 28 bytes

d.select{|x|d.count(x)>1}|[]


Saved 2 bytes thanks to Kirill L.

• Save 2 bytes: d.select{|x|d.count(x>1)}|[] Aug 30 '18 at 9:05
• I think you mean count(x)>1, not count(x>1). Dec 5 '20 at 0:48

# Common Lisp, 57 bytes

(remove-duplicates(remove-if(lambda(x)(<(count x d)2))d))


# Octave, 33 bytes

[~,a]=unique(d);d(a)=[];unique(d)

• Finds the indices of the first occurrence of each unique integer,
• removes those occurrences, and
• finds the unique elements of the remaining array.

Here it is on ideone. I've wrapped the snippet in a function so I could call it using all of the sample inputs.

Java 8, 80 Bytes

x.stream().filter(i->x.indexOf(i)!=x.lastIndexOf(i)).collect(Collectors.toSet())


Assuming x contains the input List of numbers.

# PHP, 35 37 bytes

Pretty straight forward:

array_diff_key($a,array_unique($a))


As a note: I didn't add the ; at the end of the line, as the questions states:

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

So this snippet could be used like this and evaluates to the correct result:

print implode(' ', array_diff_key($a,array_unique($a)));


Another note

The code above works for all test cases provided in the challenge. In those all non-unique characters are at most duplicates. If a element can occur more than two times, another array_unique() would be necessary, which increases the length to 49 bytes:

array_unique(array_diff_key($a,array_unique($a)))


Edits

• Saved 2 bytes by replacing array_diff_assoc with array_diff_key. Thanks to Jörg Hülsermann.
• array_diff_key instead array_diff_assoc` May 8 '17 at 17:48
• @JörgHülsermann Good catch. Thanks. Will take a look at your other suggestions within the next days. May 10 '17 at 14:20