52
\$\begingroup\$

In this question at Code Review they tried to find the fastest way to return the unique element in an array where all the elements are the same except one. But what is the shortest code that accomplish the same thing?

Goal

Find the unique element in an array and return it.

Rules

  • The input array will contain only integer, strictly positive numbers, so you can use 0 as the end of the input if your language needs it.
  • The size of the array will be at least 3 and will have a finite size. You can limit the size of the array to any limit your language has.
  • Every element in the array will be the same, except for one which will be different.
  • You must output the value (not the position) of the unique element in any standard format. You can output leading or trailing spaces or newlines.
  • You can take the input array in any accepted format.

Examples

Input                   Output
------------------------------
[ 1, 1, 1, 2, 1, 1 ]      2
[ 3, 5, 5, 5, 5 ]         3
[ 9, 2, 9, 9, 9, 9, 9 ]   2
[ 4, 4, 4, 6 ]            6
[ 5, 8, 8 ]               5
[ 8, 5, 8 ]               5
[ 8, 8, 5 ]               5

Winner

This is , so may the shortest code for each language win!

\$\endgroup\$
7
  • \$\begingroup\$ Do we assume the input array contains a unique element and others are the same? \$\endgroup\$
    – Amessihel
    Commented Jul 28, 2020 at 14:52
  • \$\begingroup\$ @Amessihel yes, you don't need to check that, it is assumed. \$\endgroup\$
    – Charlie
    Commented Jul 28, 2020 at 14:54
  • 8
    \$\begingroup\$ The title feels too long for such a simple operation. Suggested: Odd one out \$\endgroup\$
    – Luis Mendo
    Commented Jul 28, 2020 at 15:05
  • 7
    \$\begingroup\$ @LuisMendo nice title, the thing is that I just wanted to use the same title as the original question at Code Review but changing "fastest" with "shortest". :-) \$\endgroup\$
    – Charlie
    Commented Jul 28, 2020 at 15:08
  • 1
    \$\begingroup\$ Suggest adding testcases [8, 8, 5] and [5, 8, 8] since there isn't one were the last element is unique and 3 element arrays are a corner cases in themselves. \$\endgroup\$
    – Noodle9
    Commented Jul 28, 2020 at 19:19

92 Answers 92

25
\$\begingroup\$

Python 3, 27 bytes

lambda x:min(x,key=x.count)

Try it online!

\$\endgroup\$
14
\$\begingroup\$

R, 24 bytes

a=scan();a[a!=median(a)]

Try it online!

\$\endgroup\$
1
  • 5
    \$\begingroup\$ Alas for the lack of a built-in mode function. \$\endgroup\$
    – svavil
    Commented Jul 28, 2020 at 22:32
13
\$\begingroup\$

JavaScript (ES6),  32  27 bytes

Saved 5 bytes thanks to @xnor

a=>a.sort()[0]+a.pop()-a[1]

Try it online!

How?

a.sort() sorts the input array in lexicographical order. We know for sure that this is going to put the unique element either at the first or the last position, but we don't know which one:

[ x, ..., y, ..., x ].sort() -> [ y, x, ..., x ] or [ x, x, ..., y ]

Either way, the sum of the first and the last elements minus the 2nd one gives the expected result:

[ y, x, ..., x ] -> y + x - x = y
[ x, x, ..., y ] -> x + y - x = y

We could also XOR all of them:

a=>a.sort()[0]^a.pop()^a[1]

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Here's a bit of a different strategy that I think works: TIO \$\endgroup\$
    – xnor
    Commented Jul 28, 2020 at 15:17
  • \$\begingroup\$ @xnor This is much better indeed! \$\endgroup\$
    – Arnauld
    Commented Jul 28, 2020 at 15:21
10
\$\begingroup\$

x86-16 machine code, 14 bytes

Binary:

00000000: 498d 7c01 f3a6 e305 4ea6 7401 4ec3       I.|.....N.t.N.

Listing:

49          DEC  CX             ; only do length-1 compares 
8D 7C 01    LEA  DI, [SI+1]     ; DI pointer to next value 
F3 A6       REPE CMPSB          ; while( [SI++] == [DI++] );
E3 05       JCXZ DONE           ; if end of array, result is second value
4E          DEC  SI             ; SI back to first value
A6          CMPSB               ; [SI++] == [DI++]? 
74 01       JE   DONE           ; if so, result is second value
4E          DEC  SI             ; otherwise, result is first value 
        DONE: 
C3          RET                 ; return to caller 

Callable function, input array in [SI], length in CX. Result in [SI].

Explanation:

Loop through the array until two different adjacent values are found. Compare the first to the third value. If they are the same, the "odd value out" must be the second, otherwise it is the first.

Example:

Input [ 1, 1, 1, 2, 1, 1 ], reduce until different adjacent values are found a = [ 1, 2, 1, 1 ]. If a[0] == a[2] then result is a[1], otherwise result is a[0].

Tests using DOS DEBUG:

enter image description here

enter image description here

enter image description here

enter image description here

\$\endgroup\$
1
  • 1
    \$\begingroup\$ lea di, [si+1] is the same size but saves instructions. IDK if we could justify taking the array length as "max index", avoiding the dec cx. It's not an "interesting" saving, just calling-convention wanking, so not worth updating your test-result images. And hard to justify; it's so standard to do pointer+length even in asm. Nice idea overall to use rep cmpsb this way. \$\endgroup\$ Commented Jul 31, 2020 at 3:40
9
\$\begingroup\$

05AB1E, 2 bytes

ʒ¢

Try it online!

Since 1 is the only truthy integer in 05AB1E, we can just filter (ʒ) on the ¢ount.

There is also the Counter-Mode builtin, which returns the least frequent element in a list at the same bytecount:

.m

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Stax also has a built-in, but it outputs a code point... \$\endgroup\$ Commented Jul 28, 2020 at 15:04
  • \$\begingroup\$ When I read the challenge just yet I had ¢Ï in mind, which is basically the same approach as your first. Didn't think about the least frequent builtin, but that's indeed a nice alternative. :) \$\endgroup\$ Commented Aug 4, 2020 at 7:01
  • \$\begingroup\$ @KevinCruijssen thats neat, I had the same idea, but couldn't find Ï in the documentation. Maybe I just need to read all commands descriptions at some point ;). \$\endgroup\$
    – ovs
    Commented Aug 4, 2020 at 7:27
8
\$\begingroup\$

APL (Dyalog Extended), 5 bytes

⍸1=¯⍸

Try it online!

Regular Dyalog APL 18.0 has ⍸⍣¯1, but it doesn't work here because it requires the input array to be sorted, unlike Extended's ¯⍸ which allows unsorted input arrays.

How it works

⍸1=¯⍸  ⍝ Input: a vector N of positive integers
       ⍝ (Example: 4 4 6 4)
   ¯⍸  ⍝ Whence; generate a vector V where V[i] is the count of i in N
       ⍝ (¯⍸ 4 4 6 4 → 0 0 0 3 0 1)
 1=    ⍝ Keep 1s intact and change anything else to zero (V1)
       ⍝ (1= 0 0 0 3 0 1 → 0 0 0 0 0 1)
⍸      ⍝ Where; generate a vector W from V1, where i appears V1[i] times in W
       ⍝ (⍸ 0 0 0 0 0 1 → 6)
\$\endgroup\$
8
\$\begingroup\$

Haskell, 32 bytes

f(x:y)|[e]<-filter(/=x)y=e|1<3=x

Try it online!

Explanation

The code, expanded with variables renamed to be more descriptive.

f (first:rest)
 | [unique] <- filter (/=first) rest = unique
 | 1 < 3 = first

(first:rest) is a pattern match on a list that destructures it into its first element (first) and the list without the first element (rest).

Each line with a | at the front is a case in the function (known as "guards" in Haskell). The syntax looks like functionName args | condition1 = result1 | condition2 = result2 .... There are two cases:

  1. [unique] <- filter (/=first) rest. This asserts that filter (/=first) rest produces a list containing only one element, which we name unique. filter (/=first) rest filters out all elements in rest not equal to first. If we are in this case, then we know that unique is the unique element, and we return it.
  2. 1 < 3. This asserts that 1 is less than 3. Since it's always true, this is a "fallthrough" case. If we reach it, we know that there are at least 2 elements not equal to the first element, so we return first.
\$\endgroup\$
8
\$\begingroup\$

Bash + coreutils, 12 bytes

sort|uniq -u

Try it online!

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

Jelly, 3 bytes

ḟÆṃ

Explanation: (probably) removes all elements that are not the most common element (returned by Æṃ). I don't know why isn't the result a single-element list (perhaps it's a feature I didn't know about?), but that makes this even better.

Try it online!

\$\endgroup\$
1
  • 3
    \$\begingroup\$ The result of the Link ḟÆṃ is a single-element-list, it's just that when a full-program is run which results in a single-element-list the printed output is what would be printed if that single-element were the result. (i.e. 2RWWW just prints [1,2] rather than [[[[1,2]]]]. \$\endgroup\$ Commented Jul 28, 2020 at 17:41
6
\$\begingroup\$

J, 10 8 bytes

1#.|//.~

Try it online!

How it works

 1#.|//.~  input:                  1 1 1 2 1
      /.~  group by itself:        1 1 1 1
                                   2
    |/     insert | (remainder) into both groups:        
                   1 | 1 | 1 | 1 = 0
                   2             = 2
 1#.       sum:                    2
\$\endgroup\$
0
6
\$\begingroup\$

Octave, 17 bytes

@(x)x(x!=mode(x))

Try it online!

2 bytes golfed thanks to Luis Mendo!

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

Ruby, 18 bytes

->a{a-[a.sort[1]]}

Try it online!

The unique element will always be the largest or smallest, so remove all copies of the second element.

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

Octave / MATLAB, 19 bytes

@(x)x(sum(x==x')<2)

Try it online!

How it works

@(x)                  % Define a function of x
          x==x'       % Square matrix of all pairwise comparisons (non-complex x)
      sum(     )      % Sum of each column
                <2    % Less than 2? This will be true for only one column
    x(            )   % Take that entry from x
\$\endgroup\$
3
  • \$\begingroup\$ this is the same byte count if you were to port the R answer @(x)x(x!=median(x)) \$\endgroup\$
    – Giuseppe
    Commented Jul 28, 2020 at 15:32
  • 2
    \$\begingroup\$ @Giuseppe Good idea! And then @(x)x(x~=mode(x)) is two bytes shorter. You should post that yourself \$\endgroup\$
    – Luis Mendo
    Commented Jul 28, 2020 at 15:36
  • 1
    \$\begingroup\$ I can't believe I didn't think of that myself!! Thanks! \$\endgroup\$
    – Giuseppe
    Commented Jul 28, 2020 at 15:44
6
\$\begingroup\$

Wolfram Language (Mathematica), 21 19 17 bytes

f[c=a_...,b_,c]=b

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ also Tally[#][[1, 1]] & for 18 \$\endgroup\$
    – Kai
    Commented Jul 30, 2020 at 20:27
  • \$\begingroup\$ @Kai Tally lists elements in order of first appearance. That function is equivalent to #[[1]]&. (besides that, it also contains redundant whitespace) \$\endgroup\$
    – att
    Commented Jul 30, 2020 at 20:41
  • \$\begingroup\$ oops you're right, didn't test enough, I thought it would sort them by the number of times they appear. White space was inserted when I copied it out of mma \$\endgroup\$
    – Kai
    Commented Jul 30, 2020 at 20:54
5
\$\begingroup\$

R, 25 bytes

a=sort(scan());a[a!=a[2]]

Try it online!

Sort the input, giving a. Now a[2] is one of the repeated values. Keep only the element not equal to a[2].

This ends up 4 bytes shorter than my best shot with a contingency table:

names(which(table(scan())<2))
\$\endgroup\$
3
  • \$\begingroup\$ How do you know that a[2] is one of the repeated values and not the unique value? After sorting, the unique value must be either the first or last element. But couldn't a[2] be the last element? \$\endgroup\$
    – Stef
    Commented Jul 29, 2020 at 12:05
  • 3
    \$\begingroup\$ @Stef a[2] is the second element (R uses 1-based indexing), and the array of guaranteed to have at least 3 elements. \$\endgroup\$
    – Giuseppe
    Commented Jul 29, 2020 at 12:40
  • \$\begingroup\$ Thanks! I didn't know that R used 1-indexing. \$\endgroup\$
    – Stef
    Commented Jul 29, 2020 at 12:46
5
\$\begingroup\$

Pyth, 4 bytes

-1 byte thanks to @FryAmTheEggman

ho/Q

Try it online!

Explanation

 o   : Order implicit input
  /Q : by count of the element
h    : then take the first element
\$\endgroup\$
0
5
\$\begingroup\$

PowerShell, 23 bytes

$args-ne($args|sort)[1]

Try it online!

Takes input by splatting. We apply a filter to the original array getting rid of the dupes by sorting it and taking the 2nd element.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ omg - ($args|group|where count -eq 1).name doesn't even come close :( \$\endgroup\$ Commented Jul 29, 2020 at 9:38
  • 2
    \$\begingroup\$ and $args|group|? c* -eq 1|% n* too \$\endgroup\$
    – mazzy
    Commented Jul 29, 2020 at 10:45
5
\$\begingroup\$

T-SQL, 40 bytes

Input is a table variable

DECLARE @ table(v int)
INSERT @ values(1),(1),(2),(1)

SELECT*FROM @ 
GROUP BY v
HAVING SUM(1)=1

Another variation

T-SQL, 54 bytes

DECLARE @ table(v real)
INSERT @ values(1),(1),(2),(1)

SELECT iif(max(v)+min(v)<avg(v)*2,min(v),max(v))FROM @
\$\endgroup\$
2
  • 1
    \$\begingroup\$ This is so straightforward and logical in SQL. Nice! \$\endgroup\$
    – 640KB
    Commented Aug 2, 2020 at 13:07
  • \$\begingroup\$ @640KB thanks for your comment. I added a more obscure solution as well, it is a bit longer unfortunately . However this syntax could be converted to other languages and hopefully allow short answers \$\endgroup\$ Commented Aug 3, 2020 at 11:10
5
\$\begingroup\$

Brachylog, 4 bytes

oḅ∋≠

Try it online!

Takes a list and returns a singleton list.

Explanation

oḅ∋≠  Input is a list, say [4,4,3,4].
o     Sort: [3,4,4,4]
 ḅ    Blocks of equal elements: [[3],[4,4,4]]
  ∋   Pick a block: [3]
   ≠  This block must have distinct elements (in this case, must have just one).
      Output it implicitly.
\$\endgroup\$
4
\$\begingroup\$

RAD, 13 bytes

⍵[1⍳⍨+/⍵∘=¨⍵]

The lack of a need for {} in this language really helps.

Try it online!

Explanation

⍵[1⍳⍨+/⍵∘=¨⍵]
     +/⍵∘=¨⍵  count of each element's # of occurrences
  1⍳⍨         first occurrence of a 1
⍵[          ] the argument at that index
\$\endgroup\$
4
\$\begingroup\$

Haskell, 30 bytes

f(x:y)|elem x y=f$y++[x]|1>0=x

Try it online! Footer stolen from coles answer.

If the first element is not in the remaining list, return it, otherwise rotate it to the end and try again.

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

K (ngn/k), 8 6 bytes

Solution:

*<#'=:

Try it online!

Explanation:

*<#'=: / the solution
    =: / group the input
  #'   / count length of each
 <     / sort ascending
*      / take the first

Extra:

  • -2 bytes thanks to ngn by dropping the lambda
\$\endgroup\$
2
  • 1
    \$\begingroup\$ *<#'=: is valid too \$\endgroup\$
    – ngn
    Commented Jul 29, 2020 at 9:20
  • \$\begingroup\$ Ah nice, I was trying to get it to work without the lambda, will update! \$\endgroup\$
    – mkst
    Commented Jul 29, 2020 at 9:26
4
\$\begingroup\$

Wolfram Language (Mathematica), 19 bytes

Tr[#/.Median@#->0]&

Inspired by Kirill L.'s R submission

Try it online!

Finds the median, replaces its occurrences in the list with 0, and sums the list.

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

Husk, 3 2 bytes

-1 byte thanks to Razetime!

◄=

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 2 bytes \$\endgroup\$
    – Razetime
    Commented Oct 15, 2020 at 5:47
4
\$\begingroup\$

Vyxal, 3 bytes

Ċ↓h

Try it Online!

Explained

Ċ↓h
Ċ     # Get the counts of all items
 ↓    # Get the smallest item based on last item
  h   # Output the head of that list

Also, according to code-golf's statistics, only a small percentage of people who view my answers actually upvote them. So if you enjoy this answer, consider upvoting, it's free, and you can change your mind at any time (given you have ≥2k rep). Enjoy the answer.

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

Julia 1.0, 24 bytes

~d=d[@.sum(==(d),[d])<2]

Try it online!

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

Thunno 2, 2 bytes

Ṁo

Note that output as a singleton list is a default.

Explanation

Ṁo  # Implicit input
 o  # Remove all instances of                       from the input
Ṁ   #                         the mode of the input
    # Implicit output

Screenshot

Screeshot

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

ATOM, 15 chars, 18 bytes

Code:

*🔍2>🧵($1🔍$1==*)

Usage:

[1,1,1,2,1,1] INTO *🔍2>🧵($1🔍$1==*)

Try it live here

Explanation:

The ATOM language implicitly treats the expression as a function, where * is the parameter.

Therefore, it interprets the expression as

[1,1,1,2,1,1] 🔍2>🧵($1🔍$1==*)

The 🔍 operator is an array filter, which takes an array on the left, and a function on the right. The expression to the right is implicitly parsed into a function. Therefore, it is searching the values in the input array for all values where the following returns true

2>🧵($1🔍$1==*)

The 🧵 operator is array length, so it is searching for elements in the original input where ($1🔍$1==*) evaluates to an array of length 1 i.e. the unique element.

The first $1 looks for the variable on the stack at index 1, which is the original array (index 0 is the element in the array that is being iterated through, since we are inside the where operator).

Another 🔍 operator is performed on the array. $1==* is now parsed to a new function, and $1 refers to the variable on the stack at index 1, which is the number being iterated through in the outer 🔍. The * refers to the immediate stack variable, which is the number being iterated through in the inner 🔍. Matches are returned, so if there are multiple instances of a number in the original array, the resulting array will be of length greater than or equal to 2. As such, only the unique element in the array will satisfy the 2>🧵($1🔍$1==*) clause.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to CGCC! And excellent answer! It can even detect multiple unique values! \$\endgroup\$ Commented May 31, 2023 at 9:30
4
\$\begingroup\$

Uiua, 4 bytes

⊗1⍘⊚

Try it!

⊗1⍘⊚
  ⍘⊚  # inverse where, i.e. how many times does each element occur?
⊗1    # index of 1
\$\endgroup\$
4
\$\begingroup\$

AWK, 34 bytes

{a[$0]++?c=$0:b+=$0}END{print b-c}

Attempt This Online!

Takes positive integers separated by newlines. Let's call the unique number u and duplicates d.

  • {a[$0]++?c=$0:b+=$0}: Executed for each line. $0 contains the number on the line.
    • a is used as a dict that counts the occurrences of each number. a[$0]++ increments the count, and evaluates to falsy if it was encountered the first time, truthy otherwise.
    • b+=$0 is run exactly twice, when each of u and d is read the first time. Therefore, b is equal to u+d at the end.
    • c=$0 is run for the rest of the time, and $0 in this case is always d.
  • END{print b-c} is run after all inputs are processed. b-c is u, and it is printed.
\$\endgroup\$
1

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.