26
\$\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\$
6
  • 5
    \$\begingroup\$ Related \$\endgroup\$
    – Sp3000
    Commented Oct 13, 2015 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
    Commented Oct 13, 2015 at 10:21
  • 2
    \$\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\$ Commented Oct 13, 2015 at 15:01
  • 1
    \$\begingroup\$ Does the output have to be a list or would a set be acceptable? \$\endgroup\$
    – Dennis
    Commented Oct 13, 2015 at 15:06
  • \$\begingroup\$ And does it have to output in that format? \$\endgroup\$ Commented Oct 13, 2015 at 15:06

60 Answers 60

18
\$\begingroup\$

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

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
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 13: q~$e`{((<~}%p \$\endgroup\$
    – Sp3000
    Commented Oct 13, 2015 at 10:29
  • 3
    \$\begingroup\$ @Sp3000 I found another 13-byte version before reading your comment :) It preserves the order too. \$\endgroup\$ Commented Oct 13, 2015 at 10:30
9
\$\begingroup\$

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.

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

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

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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.) \$\endgroup\$
    – manatwork
    Commented Oct 14, 2015 at 10:04
  • \$\begingroup\$ @manatwork cheers for that, sql server also understands the select*from. Doesn't like the 1having though.. will leave that as I having \$\endgroup\$
    – MickyT
    Commented Oct 14, 2015 at 18:25
6
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

JavaScript (ES6), 37 bytes

Run this in the JavaScript console:

e={};d.filter(x=>(e[x]=1+e[x]||0)==1)
\$\endgroup\$
4
  • \$\begingroup\$ 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! \$\endgroup\$
    – Mwr247
    Commented Oct 13, 2015 at 14:08
  • 1
    \$\begingroup\$ @Mwr247 The question states that the anwer may be a snippet which evaluates to the correct result. \$\endgroup\$ Commented Oct 13, 2015 at 14:09
  • 1
    \$\begingroup\$ It seems I misinterpreted that paragraph. Apologies then =) \$\endgroup\$
    – Mwr247
    Commented Oct 13, 2015 at 14:12
  • \$\begingroup\$ @Mwr247 No problem! :) \$\endgroup\$ Commented Oct 13, 2015 at 14:13
6
\$\begingroup\$

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.

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

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)}
\$\endgroup\$
3
  • \$\begingroup\$ SyntaxError: invalid syntax for Python 3 is it valid only for 3.5? When has python started becoming esoteric. \$\endgroup\$
    – garg10may
    Commented Oct 14, 2015 at 2:08
  • \$\begingroup\$ @garg10may Just wait til you see what 3.6 has in store... \$\endgroup\$
    – Sp3000
    Commented Oct 14, 2015 at 7:45
  • 1
    \$\begingroup\$ @Sp3000 Awesome. Looks like the same set-up as Scala. Infinitely more readable than more alternatives. \$\endgroup\$ Commented Oct 15, 2015 at 18:19
6
\$\begingroup\$

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ć

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think you can replace $_.Count-gt1 with $_.Count-1 which would be true for any Count greater than one. \$\endgroup\$ Commented Oct 13, 2015 at 22:03
  • \$\begingroup\$ @DankoDurbić Excellent! \$\endgroup\$ Commented Oct 14, 2015 at 12:39
6
\$\begingroup\$

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)

\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

Octave, 22 bytes

Based on Luis Mendo's answer.

d(sum(triu(d==d'))==2)
\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ That looks beautiful! But the 4th test case doesn't seem to be correct... \$\endgroup\$ Commented Oct 13, 2015 at 14:51
  • 1
    \$\begingroup\$ You can remove which since [ also accepts a logical argument. \$\endgroup\$
    – flodel
    Commented Oct 14, 2015 at 4:31
4
\$\begingroup\$

Mathematica, 31 29

Cases[{s_,t_/;t>1}:>s]@*Tally
\$\endgroup\$
4
\$\begingroup\$

Shell + GNU coreutils, 12

sort|uniq -d

Test output:

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

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.

\$\endgroup\$
4
\$\begingroup\$

LINQ,62 54 bytes

Kinda new here, but here goes nothing.

d.GroupBy(c=>c).Where(g=>g.Count()>1).Select(g=>g.Key)
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to the site! I don't know LINQ, but there's some whitespace you can probably remove from this to improve your score. \$\endgroup\$
    – DLosc
    Commented Oct 14, 2015 at 2:05
4
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ this will output set not a list \$\endgroup\$
    – garg10may
    Commented Oct 13, 2015 at 12:49
  • \$\begingroup\$ So shall I surround my snippet with a call to list(...) ? \$\endgroup\$
    – dieter
    Commented Oct 13, 2015 at 13:33
  • \$\begingroup\$ Yes the output should be an array only. \$\endgroup\$
    – garg10may
    Commented Oct 14, 2015 at 1:59
4
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – coltim
    Commented Dec 7, 2020 at 19:12
4
\$\begingroup\$

Python 3 - 33 30 bytes

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

Repl output, d as input.

\$\endgroup\$
4
\$\begingroup\$

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

Haskell + hgl, 11 10 bytes

(!!1)~<<bg

Attempt This Online!

Explanation

  • bg sort a list into lists of like values.
  • (!!1) get the second value or [] if there is none.

We use ~<< to glue these together, which basically acts as a concat map.

Reflection

This works pretty well, but the operator precedence is killing me. Both of the following work but requires the parens to prevent an error.

nb<(df^*nb)
nb<(fdf~<nb)

If the operator precedence was different both of these would be competitive with the first one actually saving bytes.

I don't know if that means the precedence is wrong, but it's worth noticing.

(!!) should have a prefix function, and a bunch of versions with the index prefilled. I noticed this earlier but still didn't fix it, and now it's coming back to haunt me.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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

Ruby, 30 28 bytes

In the Interactive Ruby Shell:

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

Saved 2 bytes thanks to Kirill L.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Save 2 bytes: d.select{|x|d.count(x>1)}|[] \$\endgroup\$
    – Kirill L.
    Commented Aug 30, 2018 at 9:05
  • 2
    \$\begingroup\$ I think you mean count(x)>1, not count(x>1). \$\endgroup\$ Commented Dec 5, 2020 at 0:48
3
\$\begingroup\$

Husk, 4 bytes

u-u¹

Try it online!

u       # unique element of
 -      # set difference between
  u¹    # unique elements of input  
        # and input
\$\endgroup\$
3
\$\begingroup\$

Thunno D, \$7 \log_{256}(96) \approx\$ 5.76 bytes

Note: this only works on Thunno 1.1.3+

gc1>kZU

(No ATO link since that's on v1.1.0 at the moment)

Screenshot

Explanation

gc1>kZU   # Implicit input. The D flag duplicates it implicitly.
g   k     # Filter the input for elements where the following is truthy:
 c        #   The count of the element in the input
  1>      #   Is greater than 1
     ZU   # Deduplicate and output implicitly
\$\endgroup\$
3
\$\begingroup\$

JavaScript, 35 28 34 bytes

a=>a.filter(o=x=>(o[x]=-~o[x])==2)

Try It Online!

\$\endgroup\$
2
  • \$\begingroup\$ Return [0,0,0] for [0,0,0,0,0,0], intended? \$\endgroup\$
    – l4m2
    Commented Jan 6, 2023 at 4:36
  • \$\begingroup\$ @l4m2, fixed that. \$\endgroup\$
    – Shaggy
    Commented Jan 6, 2023 at 10:27
2
\$\begingroup\$

Common Lisp, 57 bytes

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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