| Bytes | Lang | Time | Link |
|---|---|---|---|
| 003 | Vyxal | 250124T074505Z | emanresu |
| 723 | ☾ | 250124T055622Z | Ganer |
| 007 | Haskell + hgl | 250105T222231Z | Wheat Wi |
| 005 | iogii | 250105T203859Z | Darren S |
| 006 | Uiua | 250105T154954Z | Eric Sch |
| 008 | K ngn/k | 231015T230416Z | Bubbler |
| 010 | K ngn/k | 231015T204604Z | coltim |
| 004 | Jelly | 231015T201514Z | Unrelate |
| 007 | Uiua | 231015T192358Z | chunes |
| 067 | Regex ECMAScript | 190313T131816Z | Deadcode |
| 025 | Factor | 210402T050829Z | chunes |
| 007 | GolfScript | 200309T132517Z | Mathgeek |
| 096 | Javascript | 190312T105214Z | Jeyanth |
| 049 | F# | 190311T214741Z | Ciaran_M |
| 038 | R | 190311T210538Z | Chthonyx |
| 007 | APL Dyalog Unicode | 190302T082124Z | voidhawk |
| 016 | Pari/GP | 190302T083647Z | alephalp |
| 007 | Brachylog | 190302T030524Z | Unrelate |
| 007 | Japt | 180524T203129Z | Etheryte |
| 031 | 12basic | 180524T202044Z | 12Me21 |
| 040 | R | 180524T190824Z | JayCe |
| 037 | PowerShell | 171011T202746Z | AdmBorkB |
| 044 | Java 8 | 171012T075945Z | Kevin Cr |
| 031 | R | 171011T212222Z | Giuseppe |
| 012 | Perl 6 | 171013T213246Z | Sean |
| 031 | Clojure | 171012T220731Z | Gabe Lau |
| 131 | Batch | 171012T210042Z | Slapparo |
| 035 | Bash | 171012T124046Z | Nahuel F |
| 042 | Excel | 171012T123004Z | pajonk |
| nan | Perl 5 | 171012T114703Z | Nahuel F |
| 005 | 05AB1E | 171011T203547Z | Arnauld |
| 078 | C GCC | 171012T024849Z | pizzapan |
| 3518 | C# .NET Core | 171012T103345Z | Ian H. |
| 011 | Retina | 171011T202036Z | Martin E |
| 046 | PHP | 171012T092007Z | roberto0 |
| 109 | Batch | 171012T091130Z | Neil |
| 026 | Octave | 171012T055250Z | Stewie G |
| 034 | Haskell | 171012T063121Z | Laikoni |
| 006 | Pyth | 171012T043014Z | Mr. Xcod |
| 026 | Julia | 171011T233833Z | eaglgene |
| 006 | J | 171011T203345Z | FrownyFr |
| 032 | Ruby | 171012T010443Z | parenpar |
| 025 | Jq 1.5 | 171011T235839Z | jq170727 |
| 021 | Octave / MATLAB with Statistics package / toolbox | 171011T215450Z | Luis Men |
| 036 | Bash + coreutils | 171011T223601Z | Dennis |
| 025 | Octave | 171011T203631Z | flawr |
| 037 | Haskell | 171011T215440Z | H.PWiz |
| 005 | 05AB1E | 171011T214544Z | Riley |
| 006 | MATL | 171011T210058Z | DJMcMayh |
| 004 | 05AB1E | 171011T212758Z | Luis Men |
| nan | Perl 5 | 171011T212436Z | Xcali |
| 005 | Jelly | 171011T201648Z | caird co |
| 004 | Husk | 171011T204425Z | Zgarb |
| 037 | Haskell | 171011T205311Z | flawr |
| 008 | Pushy | 171011T203634Z | FlipTack |
| 030 | JavaScript ES6 | 171011T203912Z | Shaggy |
| 007 | Convex | 171011T203531Z | Erik the |
| 026 | Mathematica | 171011T203353Z | ZaMoC |
| 007 | Japt | 171011T202713Z | Shaggy |
| 010 | Jelly | 171011T201837Z | hyperneu |
| 028 | Python 3 | 171011T201541Z | LyricLy |
Vyxal, 3 bytes
UǏl
I have never used this overload of l before and likely never will again.
U # Does uniquifying the input
Ǐ # And appending the first item
l # Have the same length as the original input?
Haskell + hgl, 7 bytes
eL2<xuq
Explanation
xuqremove all unique elements from the inputeL2determine if the length is 2.
No xuq, 10 bytes
xuq makes the challenge a little boring, so I tried without it too.
lEq~<K1<nb
Explanation
nbremove duplicate elements of the inputK1add a dummy element on the front (it's a1but that doesn't matter)lEqdetermine if the modified list has the same length as the original
If the input satisfies the requirement, then removing duplicates should remove exactly one element. And so adding a dummy element brings us back to the original length iff the input satisfies the requirement.
No xuq or nb, 15 bytes
What if we also didn't have nb?
eq4<pd<(m~<fce)
Explanation
m~<fcecount the number of times each element occurs in the inputpdtake the producteq4determine if that is equal to 4.
If the product is 4 then the input must have 2 elements appearing twice and some number of elements appearing once. If all the elements are unique then the product is 1, if there's any more equal pairs then the product will be greater than 4.
Fun fact: the numbers that can appear as the output of pd<(m~<fce) are A072873.
Reflection
So the main answer here is pretty tight. But I have some thoughts.
- As weird as it is, I think
(m~<)is actually a common enough combination. It basically takes a function and then supplies it withxand maps the result acrossx. - Something to get all the sizes of groups would probably be useful.
iogii, 5 bytes
?n#1=
Check that there is exactly one value that isn't the first occurrence
? debut (is first occurrence)
n not
# sum
1
=
Uiua, 6 bytes
=1/+¬◰
=1/+¬◰
¬◰ # Mask of first occurrences of items in an array then negation
/+ # number of duplicate items
=1 # is it equal to one?
K (ngn/k), 8 bytes
2=*/#'=:
Checks if the product of the counts of unique elements is 2. This works because the only way to get the product of 2 is (1, 1, 1, ..., 1, 2) in some order.
K (ngn/k), 10 bytes
1=-/#'1?:\
A tacit version of {1=(#x)-#?x}.
1?:\run a distinct-do-scan for one iteration, returning a two-item list containing the original input and the distinct items of the input#'take the count/length of each item in that list1=-/is the difference between the counts one?
Jelly, 4 bytes
QṁƑḊ
Q Is the input uniquified
ṁƑ the same length as
Ḋ the input without its first element?
Uiua, 7 bytes
=1-⧻⊃⊝⧻
=1-⧻⊃⊝⧻
⊃⊝⧻ # length and unique elements of input
⧻ # length (of unique elements)
- # subtract
=1 # is it equal to one?
Regex (ECMAScript), 69 67 bytes
The input is in the form of a comma-delimited list of nonnegative integers in decimal.
^(?=.*\b(\w+\b).*,\1\b)(?!(.*\b\1\b){3}|.*\b(?!\1\b)(\w+\b).*,\3\b)
^
(?=
.*\b(\w+\b) # \1 = an element that occurs at least twice
.*,\1\b # locate the second occurrence of \1; since there's at least one
# element before it, it's guaranteed to have a commma in front
)
(?!
(.*\b\1\b){3} # Assert that \1 does not occur 3 or more times
|
.*\b(?!\1\b)(\w+\b) # \3 = any element that's different from \1
.*,\3\b # Assert that \3 does not occur again; since there's at least one
# element before it, it's guaranteed to have a commma in front
)
Regex (ECMAScript), 67 65 bytes
^(?=.*(\bx+\b).*,\1\b)(?!(.*\b\1\b){3}|.*\b(?!\1\b)(x+\b).*,\3\b)
This is a straight port of the decimal version to positive unary, changing both occurrences of \w+ to x+. Input is a list of positive unary numbers separated by ,. Each one is a string of xs whose length represents the number.
Regex (ECMAScript), 92 bytes
This version fully supports nonnegative unary. That is very costly; \b can no longer be used at all. The part that ballooned the most in length was where it's asserted that no element other than \2 is repeated 3 or more times, because with a simple {3} loop, it would get a false negative due to falsely finding a repeated zero consisting of 3 repeated zero-width matches with no commas consumed.
^(?=.*(^|,)(x*(?!x)).*,\2(?!x))(?!.*(^|,)(\2(?!x)(.*,\2(?!x)){2}|(?!\2(?!x))(x*(?!x)).*,\6(?!x)))
^
(?=
.*(^|,)(x*(?!x)) # \2 = an element that occurs at least twice
.*,\2(?!x) # locate the second occurrence of \2; since there's at least one
# element before it, it's guaranteed to have a commma in front
)
(?!
.*(^|,)
(
\2((?!x).*,\2){2} # Assert that \2 does not occur 3 or more times
|
(?!\2(?!x))(x*(?!x)) # \6 = any element that's different from \2
.*,\6 # Assert that \6 does not occur again; since there's at least one
# element before it, it's guaranteed to have a commma in front
)
(?!x)
)
Factor, 25 bytes
[ duplicates length 1 = ]
Explanation
It's a quotation (anonymous function) that takes a sequence as input and leaves a boolean as output. Assuming { 1 6 3 4 4 7 9 } is on top of the data stack when this quotation is called...
duplicatesObtain a list of duplicate values. Stack:{ 4 }lengthGet the length. Stack:11Push1to the stack. Stack:1 1=Returntif the top two objects on the data stack are equal, otherwise returnf. Stack:t
GolfScript, 7 bytes
.,\.&,-
.,\.&,- # Stack will be show as [], with L being our list.
. # Duplicate array [L L]
, # Size of top element [L N]
\ # Swap the positions of the top two elements [N L]
.& # Listwise OR (remove duplicates) [N L&]
, # Size of top elements (should be 1 lesser) [N N-1?]
- # Difference
Output is 1 if TRUE. Any other result is FALSE.
Javascript,96 bytes
for(i=0;i<a.length;i++)for(j=0;j<a.length;j++)(a[i]==a[j]&& i!=j)?count++:0 console.log(count==2)
And a better readable format
for(i=0;i<a.length;i++){
for(j=0;j<a.length;j++){
(a[i]==a[j]&& i!=j)?count++:0
}
}
console.log(count==2)
F#, 49 bytes
let s c=Seq.distinct c|>Seq.length=Seq.length c-1
Seq.distinct creates a sequence of all the unique elements in the collection. If the number of distinct elements is one less than the original collection, there are soulmates.
R, 38 bytes
Not quite as golfed as another R answer, but a different approach.
length(a<-scan())-length(unique(a))==1
APL (Dyalog Unicode), 7 bytesSBCS
1=≢-≢∘∪
Explanation:
1=≢-≢∘∪ ⍝ Monadic function train
≢∘∪ ⍝ Generate a list of unique items in the input,
⍝ and return the length of that list
≢- ⍝ Take the length of the input and subtract the above
1= ⍝ If the difference is 1, true, otherwise false
Brachylog, 7 bytes
dl.&l-₁
Truthy/falsy input is achieved through predicate success/failure, as if this predicate is run as the entire program on a single input, it will print true. if it succeeds and false. if it fails. The header on TIO is there so you can run all of the cases at once.
dl The length of the input with duplicates removed
. is the output variable,
& and
l-₁ so is the length of the input minus 1.
Japt, 7 bytes
ÊɶUâ Ê
Ê // Return whether the number of
Uâ // unique items in the input
¶ // is equal to
ÊÉ // the input's length minus one.
12-basic, 31 bytes
FUNC S(L)?LEN(L|L)==LEN(L)-1END
The | (union) operator returns an array containing elements that are in both of the input arrays. As a side effect, it removes duplicates.
R, 40 bytes
sum(outer(x<-scan(),x,"=="))==2+sum(x|1)
Approach is different from the other R solution - and not as golfy...
PowerShell, 40 37 bytes
($args|sort -u).count-eq$args.count-1
The Sort-Object command (alias sort) with the -unique flag pulls out only the unique components of the input. For example, for input @(1,3,3,2), this will result in @(1,2,3).
Thus, we just need to make sure that the .count of this object (i.e., how many elements it has) is -equal to the .count of our input array -1 (i.e., we have exactly one duplicate entry).
Saved 3 bytes thanks to Sinusoid.
Fixed bug thanks to TessellatingHeckler.
Java 8, 46 44 bytes
l->l.stream().distinct().count()==l.size()-1
-2 bytes thanks to @Nevay. (Old answer: l->new java.util.HashSet(l).size()==l.size()-1)
Explanation:
l-> // Method with List parameter and boolean return-type
l.stream() // Stream over the List
.distinct() // ignoring all duplicated items
.count() // and get the total amount of non-duplicated items in the List
== // And check if it's size is equals to
l.size()-1 // the size of the input-list - 1
// End of method (implicit / single-line return-statement)
R, 32 31 bytes
-1 byte thanks to @JarkoDubbeldam
cat(sum(duplicated(scan()))==1)
Reads from stdin, writes to stdout.
duplicated iterates through the list, replacing the values of l with TRUE if that value occurs earlier in the list, and FALSE otherwise. If there's a unique pair of soulmates, there should be exactly one TRUE value, so the sum should be 1.
Batch, 131
set b=,%*,
goto j
:l
call set f=%%b:,%1,=,#,%%
if "%b%"=="%f%" exit 1
set b=%f%
goto:eof
:j
FOR %%A IN (%b%) DO call :l %%A
The %errorlevel% can be checked for the result. it works by tokenising the input, FOR %%A IN (%b%) DO call :l %%A every instance of that token + to commas is then replaced in a copy of the input with a ",#," call set f=%%b:,%1,=,#,%% the resulting string is then compared to the copy of the previous string. If there is no change in the string, it is because that number has already occured.
c:\>batfile.bat 1,2,3
Bash, 36, 35 bytes
for k;{ H[$k]=;};((${#H[@]}+1==$#))
TIO exit status 0: true, 1: false, ((..)) can be changed to echo $((..)), to see boolean value (1:true, 0:false)
Excel, 42 bytes
Danish language version
=TÆLV(A:A)=SUM(--(FREKVENS(A:A,A:A)>0))+1
Assumes each integer from the list in separate cell in column A.
If we were allowed for inconsistent falsey values, we could save 3 bytes:
=TÆLV(A:A)+SUM(-(FREKVENS(A:A,A:A)>0))
English language version (44 bytes)
=COUNTA(A:A)=SUM(--(FREQUENCY(A:A,A:A)>0))+1
05AB1E, 6 5 bytes
{¥>ΘO
{¥>ΘO # example input: [1, 6, 3, 4, 4, 7, 9]
{ # sort -> [1, 3, 4, 4, 6, 7, 9]
¥ # get deltas -> [ 2, 1, 0, 2, 1, 2 ]
> # increment -> [ 3, 2, 1, 3, 2, 3 ]
Θ # truthify (only 1 gives 1) -> [ 0, 0, 1, 0, 0, 0 ]
O # sum -> 1
1 being the only truthy value in 05AB1E, we can stop here. (Thanks @Emigna for pointing that out.)
To get only two distinct values, we can optionally add:
Θ # equals 1? -> 1
C (GCC), 80 78 bytes
i,j,t;f(x,l)int*x;{for(i=t=0;i<l;++i)for(j=0;j<i;)t+=x[i]==x[j++];return!~-t;}
-1 thanks to Jonathan Frech
-1 thanks to Kevin Cruijssen
f is a function that takes in an int* pointing to the list, and an int that is the length of the list, and returns 1 if there are exactly two equal entries and all other entries are distinct, and 0 value otherwise.
The function checks all pairs of numbers in the list, counting the number of pairs, and returns whether the number of pairs is 1.
C# (.NET Core), 35 + 18 bytes
+18 for using System.Linq.
n=>n.Distinct().Count()==n.Length-1
67 byte alternative without Linq:
n=>new System.Collections.Generic.HashSet<int>(n).Count==n.Length-1
Retina, 15 12 11 bytes
Thanks to Neil for saving 1 byte.
D`
Mm2`^$
1
Input is linefeed-separated. (The test suite uses comma-separation for convenience.)
Explanation
D`
Deduplicate the lines in the input, which removes any integer that has appeared before (but leaves the surrounding linefeed(s)).
Mm2`^$
Count the number of empty lines, which is equal to the number of duplicates we removed, but only consider the first two matches. So the output will only be 0 (no duplicates), 1 (one duplicate), 2 (two or more duplicates).
1
Make sure that exactly one duplicate was removed.
PHP, 46 bytes
<?=count(array_unique($argv))==count($argv)-1;
Counts the number of entries in $argv and compares it to the number of unique entries. If the former is higher than the latter by 1 then truthy, else falsey.
Batch, 109 bytes
@set/ap=1,s=0
@for %%x in (%*)do @(for %%y in (%*)do @set/a"s+=!(%%x-%%y)")&set/ap*=s,s=0
@if %p%==4 echo 1
Port of @DJMcMayhem's answer.
Octave, 23 26 bytes
@(x)prod(sum(x==x'))==4
The x==x' part was inspired by flawr's answer. This is longer than Luis' answer, but it doesn't use any toolboxes.
Explanation:
This is an anonymous function that takes a vector x as input, and compares it to itself transposed. This will give a matrix where all diagonal elements are 1, and any off diagonal elements signals that there are duplicates elements.
The sum along any given column shows how many duplicates there are of that number. We want two of the numbers to have a duplicate, so we two values equal to two, and the rest unequal to two.
If we take the product of this matrix, we'll get 4 if there are only two equal elements (2*2*1*1*1*1*...), and something other than 4 if there are no duplicates, or more than two.
Pyth, 6 bytes
qtlQl{
l{- Gets the number of unique elements.tlQ- Gets the length of the input list, decremented.q- Checks equality.
7 bytes
q1l.-Q{
Julia, 39 26 bytes
!a=sum(a.==a')==endof(a)+2
Explanation
The code generates a 2-dimensional table of booleans, which is then collected using the sum function, counting the number of same-element pairs in the cartesian square of A. Then this is compared to the length of the string plus two, and the quantities are equal only when there is exactly one repeat character.
This code redefines the NOT operator.
J, 7 6 bytes
=�,=
= check every element for equality with every unique element, creates a matrix with m rows for m unique elements.
0, add an empty row on top.
=&# does the number of rows equal the length of input?
Ruby, 32 bytes
->(s){s.uniq.length==s.length-1}
Jq 1.5, 53 25 bytes
length-(unique|length)==1
Inspired by Riley's answer and much shorter then my original solution.
Octave / MATLAB (with Statistics package / toolbox), 21 bytes
@(x)nnz(~pdist(x))==1
Anonymous function. Input is a column vector. Output is true (displayed as 1) or false (displayed as0).
Explanation
pdist(x) computes a vector of Euclidean distances between all pairs of rows from x. It considers each pair only once (order of the two rows doesn't matter), and doesn't consider pairs formed by the same row twice.
In our case x is a column vector, so Euclidean distance between two rows is just absolute difference between the two numbers.
~ is logical (Boolean) negation, nnz is number of nonzeros, and ==1 compares to 1. So the result is true if and only if there is only one pair that gives zero distance.
Bash + coreutils, 36 bytes
sort|uniq -dc|grep -Pqz '^ *2 .*\n$'
Output is via exit code, where 0 is success (truthy) and 1 is failure (falsy).
Octave, 25 bytes
This is not using a group or unique approach as many of the other answers, but rather the "cartesian product" of all possible comparisions.
@(x)nnz(triu(x==x',1))==1
Explanation
x==x' %create a matrix where the entry at (i,j) compares whether x(i) == x(ju)
triu(x==x',1) %only consider the strict upper triangular matrix
nnz(triu(x==x',1)) %count the number of nonzero entries
@(x)nnz(triu(x==x',1))==1 %check whether this number is actually 1
And because no program would be complete without a convolution (thanks @LuisMendo for fixing a mistake):
Octave, 40 bytes
@(x)nnz(~conv(sort(x),-1:2:1,'same'))==1
05AB1E, 5 bytes
gIÙg-
g # Get number of elements in input
IÙg # Get number of unique elements in input
- # Subtract
In 05AB1E 1 is the only truthy value, so for a truthy result there must be exactly 1 duplicate element removed by the uniquify.
MATL, 7, 6 bytes
&=sp4=
One byte saved thanks to @Guiseppe!
Explanation:
&= % Table of pair-wise equality comparisons
%
% [1 0 0 0 0 0 0
% 0 1 0 0 0 0 0
% 0 0 1 0 0 0 0
% 0 0 0 1 1 0 0
% 0 0 0 1 1 0 0
% 0 0 0 0 0 1 0
% 0 0 0 0 0 0 1]
%
s % Sum each Column. Stack:
%
% [1 1 1 2 2 1 1]
%
p % Product of the array. Stack:
%
% [4]
%
4= % Compare the stack to '4'
05AB1E, 4 bytes
{¥_O
Outputs 1 as truthy, any other non-negative integer as falsy. In 05AB1E, 1 is the only truthy number (thanks @Emigna for the insight!).
Explanation
{ Implicit input. Sort
¥ Consecutive differences
_ Boolean negate
O Sum. Implicit display
Jelly, 8 5 bytes
QL‘=L
Explanation
QL‘=L - Main link, argument L (a list) e.g [1,6,3,4,4,7,9]
Q - Deduplicated elements [1,6,3,4,7,9]
L - Length 6
‘ - Increment 7
L - Length of the input 7 ([1,6,3,4,4,7,9])
= - Are they equal? 1
If the output values can be any consistent values, then QL_L works, which outputs -1 for truthy and any other non-positive number for falsey (thanks @JonathanAllan)
Husk, 4 bytes
εṠ-u
Explanation
εṠ-u Implicit input.
u Unique elements.
Ṡ- Delete them from input, counting multiplicities.
ε Is the result a singleton list?
Pushy, 8 bytes
Simple implementation of checking whether len(set(list)) == len(list)-1:
LtvuL^=#
Explanation:
\ Implicit: Put all input on stack
Ltv \ Get the stack length - 1, save in auxiliary stack
u \ Remove non-unique elements
L \ Get the new length
^= \ Compare with the previously saved length
# \ Print result
This works as the length will only decrease by 1 if there was only exactly 1 non-distinct integer in the initial list.
Japt, 7 bytes
â ʶUÊÉ
Explanation
Remove duplicates (â), get length (Ê) and compare equality (¶) with the length (Ê) of the input (U) minus 1 (É).
Python 3, 30 28 bytes
lambda m:len({*m})+1==len(m)
{*m} casts the list to a set object, an unordered list of items without duplicates. Doing this will always decrease the length of the list by the number of duplicates in it. By computing how much the length has changed, we can easily tell if the list had a single duplicate and return the result of the test.
-2 bytes thanks to ovs.