| Bytes | Lang | Time | Link |
|---|---|---|---|
| 008 | APLNARS | 250913T033712Z | Rosario |
| 007 | Japt | 230106T121452Z | Shaggy |
| 005 | Uiua | 250911T213942Z | nyxbird |
| 042 | AWK | 250911T165425Z | xrs |
| 091 | Elm | 230107T051120Z | Wheat Wi |
| 058 | Curry PAKCS | 230107T134756Z | Wheat Wi |
| 034 | JavaScript | 170508T164931Z | Shaggy |
| 015 | Zsh coreutils | 230106T070749Z | roblogic |
| 022 | jq | 230106T044311Z | pmf |
| 036 | JavaScript Node.js | 230106T044256Z | l4m2 |
| 010 | Haskell + hgl | 230104T053633Z | Wheat Wi |
| nan | 230104T145116Z | The Thon | |
| 004 | 05AB1E | 180607T075600Z | Kevin Cr |
| 040 | Arturo | 230104T141428Z | chunes |
| 011 | Pip p | 230104T054828Z | DLosc |
| 010 | Factor | 221017T134040Z | chunes |
| 004 | Vyxal | 221017T132306Z | pacman25 |
| 024 | R | 201207T233955Z | Dominic |
| 004 | Husk | 201207T225447Z | Dominic |
| 018 | Perl 5 | 201207T185428Z | Xcali |
| 028 | Ruby | 151013T131335Z | Cristian |
| 009 | Burlesque | 200226T190410Z | DeathInc |
| 043 | JavaScript Node.js | 190226T152005Z | Kamil Na |
| 060 | JavaScript Node.js | 190226T150329Z | Kamil Na |
| 009 | APL Dyalog Unicode | 151013T134034Z | Adá |
| 014 | Perl 6 | 181023T234735Z | Jo King |
| 041 | Kotlin | 181023T223155Z | snail_ |
| 007 | K oK | 180828T210630Z | mkst |
| 036 | Python 2.7 | 180606T171506Z | Khalil |
| 075 | PHP | 180606T144149Z | Luis fel |
| 007 | Japt | 180606T143635Z | Oliver |
| 035 | PHP | 151015T083011Z | insertus |
| 004 | Jelly | 170508T155701Z | user6213 |
| 030 | Python 3 | 151013T125321Z | The Fift |
| 016 | Perl 6 | 151027T223225Z | Brad Gil |
| 010 | K not K5 | 151015T204932Z | kirbyfan |
| 013 | J | 151014T090232Z | marinus |
| 023 | Mathematica | 151015T073042Z | LLlAMnYP |
| 042 | SQL | 151013T182823Z | MickyT |
| 040 | C# | 151014T143916Z | Cooler R |
| 080 | Java 8 | 151014T130406Z | Rnet |
| 024 | R | 151013T135958Z | Mutador |
| 029 | PowerShell | 151013T134530Z | AdmBorkB |
| 022 | Octave | 151014T092718Z | alephalp |
| 042 | Python 2.7 | 151013T111959Z | dieter |
| 018 | STATA | 151014T043133Z | bmarks |
| 054 | LINQ | 151014T010449Z | noisyass |
| 007 | Pyth | 151013T232829Z | izzyg |
| 033 | Octave | 151013T224856Z | beaker |
| 143 | Python | 151013T224547Z | Ioannes |
| 030 | Python 3.5 | 151013T210723Z | xnor |
| 029 | Julia | 151013T175408Z | Glen O |
| 040 | Matlab / Octave | 151013T104706Z | Luis Men |
| 012 | Shell + GNU coreutils | 151013T161536Z | Digital |
| 007 | Pyth | 151013T150717Z | Dennis |
| 005 | K5 | 151013T150058Z | JohnE |
| 010 | CJam | 151013T102301Z | aditsu q |
| 104 | Hassium | 151013T140824Z | Jacob Mi |
| 037 | JavaScript ES6 | 151013T134720Z | Cristian |
| 057 | Common Lisp | 151013T131145Z | nanny |
| 032 | Haskell | 151013T105728Z | C. Quill |
| 029 | Mathematica | 151013T105930Z | alephalp |
| 026 | Mathematica | 151013T112406Z | Martin E |
APL(NARS), 8 chars
{∪⍵∼⍦∪⍵}
For this there is only to know how work the function ∼⍦.
For 2 sets ∼ make the usual A-B of set example
1 2 3 4 4 4 ∼ 1 4
2 3
instead A∼⍦B cut one element of A if it present in B and only that element
1 2 3 4 4 4 ∼⍦ 1 4
2 3 4 4
∪ unique instead return its input without element repetitions
∪1 2 3 4 4 4
1 2 3 4
so for explain better
∪⍵∼⍦∪⍵
∪⍵ unique ⍵ input
⍵∼⍦ make the difference as above, so it return only multiple elements of ⍵
∪ make list unique
one alternative is using partitions (20 chars)
{∪∊m/⍨2≤≢¨m←⊂⍨⍵[⍋⍵]} 1 4 5 1 1 4 8 3 3 8
┌4───────┐
│ 1 3 4 8│
└~───────┘
one alternative is use count elements (19 chars)
{∪⍵/⍨2≤{+/⍵=w}¨w←⍵} 1 4 5 1 1 4 8 3 3 8
┌4───────┐
│ 1 4 8 3│
└~───────┘
test:
f←{∪⍵∼⍦∪⍵}
f ⍬
f ¯1,0,1
f 1,1
1
f 3 0 0 1 1 0 5 3
0 1 3
f ¯34 0 1 ¯34 4 8 4
¯34 4
Japt, 7 bytes
ü lÉ cÎ
Try it (includes all test cases)
ü lÉ cÎ :Implicit input of array
ü :Group & sort by value
l :Filter by length
É : Minus 1
c :Flat map
Î : First element
5 bytes
If including empty elements in the output is permitted.
ü ÕÅÎ
ü ÕÅÎ :Implicit input of array
ü :Group & sort by value
Õ :Transpose
Å :Slice off first element
Î :Get first element
Elm, 91 bytes
e=List.member
g x z=case z of
a::b->g(a::x)b++if e a b&¬(e a x)then[a]else[]
_->[]
g[]
Curry (PAKCS), 58 bytes
x!(y:z)|elem y z&&all(/=y)x=y:(y:x)!z|1>0=x!z
_![]=[]
([]!)
Weirdish port of my Elm answer. Curry has nicer built in functions and pattern matching than Elm.
jq, 22 bytes
[group_by(.)[][1:2][]]
Explanation:
[ ] # collect
[] # each
[1:2] # second item
[] # from each
group_by( ) # array of arrays of
. # identical values
Haskell + hgl, 11 10 bytes
(!!1)~<<bg
Explanation
bgsort 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.
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)
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
05AB1E, 8 6 5 4 bytes
¢ÏKÙ
Try it online or verify all test cases.
Explanation:
# e.g. input=[1,2,3,2,1,1,6,4,5,6]
¢ # Count how many times each item of the (implicit) input-list occurs in the
# (implicit) input-list
# → [3,2,1,2,3,3,2,1,1,2]
Ï # Only keep the values in the (implicit) input-list at the truthy (==1) indices
# → [3,4,5]
K # Remove those values from the (implicit) input-list, leaving all non-unique items
# → [1,2,2,1,1,6,6]
Ù # Uniquify that list of non-unique items
# → [1,2,6]
# (after which the result is output implicitly)
Pip -p, 11 bytes
UQ:_Ng>1FIg
Takes the integers as command-line arguments. Attempt This Online!
Explanation
UQ:_Ng>1FIg
g List of command-line arguments
FI Filter by this function:
_N The number of times this element appears in
g the list of command-line arguments
>1 is greater than one
UQ: Uniquify the resulting list
R, 24 bytes
names((t=table(v))[t>1])
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]
One byte longer (and yet another approach) if you want to output as an integer vector.
Husk, 4 bytes
u-u¹
u # unique element of
- # set difference between
u¹ # unique elements of input
# and input
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
Ruby, 30 28 bytes
In the Interactive Ruby Shell:
d.select{|x|d.count(x)>1}|[]
Saved 2 bytes thanks to Kirill L.
Burlesque, 9 bytes
peJNB\\NB
pe # Parse and push
J # Duplicate
NB # Remove duplicate elements
\\ # Difference between nubbed and original lists
NB # Nub the difference
JavaScript (Node.js), 43 bytes (another try)
a=>a.filter((t={},e=>!(1-(t[e]=++t[e]|0))))
JavaScript (Node.js), 60 bytes
f=>[...new Set(f.filter(e=>f.indexOf(e)!=f.lastIndexOf(e)))]
APL (Dyalog Unicode), 13 9 bytesSBCS
Anonymous tacit prefix function.
∊(⊂1↓⊣¨)⌸
(…)⌸ 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)
K (oK), 7 bytes
Solution:
&1<#:'=
Explanation:
&1<#:'= / the solution
= / group, key => value (index)
#:' / count length of each group
1< / 1 less than
& / keys where true
Python 2.7, 36 bytes
list({x for x in i if i.count(x)>1})
Where i is the input
PHP, 75 bytes
function a($b){return array_unique(array_diff_assoc($b,array_unique($b)));}
Japt, 7 bytes
â £kX â
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
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_assocwitharray_diff_key. Thanks to Jörg Hülsermann.
Jelly, 4 bytes, language postdates challenge
œ-QQ
Same algorithm as the accepted answer, just a shorter syntax.
Python 3 - 33 30 bytes
{_ for _ in d if d.count(_)>1}
Repl output, d as input.
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)
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!
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@=
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.
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.
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.
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.
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.
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ć
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
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
LINQ,62 54 bytes
Kinda new here, but here goes nothing.
d.GroupBy(c=>c).Where(g=>g.Count()>1).Select(g=>g.Key)
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.
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.
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]
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)}
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.
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.
Shell + GNU coreutils, 12
sort|uniq -d
Test output:
$ printf "%s\n" -34 0 1 -34 4 8 4 | ./nonuniq.sh
-34
4
$
Pyth, 7 bytes
S{.-Q{Q
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.
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=(},_&
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
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
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
JavaScript (ES6), 37 bytes
Run this in the JavaScript console:
e={};d.filter(x=>(e[x]=1+e[x]||0)==1)
Common Lisp, 57 bytes
(remove-duplicates(remove-if(lambda(x)(<(count x d)2))d))
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.
Mathematica, 31 29
Cases[{s_,t_/;t>1}:>s]@*Tally
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.
