g | x | w | all
Bytes Lang Time Link
003Vyxal250124T074505Zemanresu
723250124T055622ZGaner
007Haskell + hgl250105T222231ZWheat Wi
005iogii250105T203859ZDarren S
006Uiua250105T154954ZEric Sch
008K ngn/k231015T230416ZBubbler
010K ngn/k231015T204604Zcoltim
004Jelly231015T201514ZUnrelate
007Uiua231015T192358Zchunes
067Regex ECMAScript190313T131816ZDeadcode
025Factor210402T050829Zchunes
007GolfScript200309T132517ZMathgeek
096Javascript190312T105214ZJeyanth
049F#190311T214741ZCiaran_M
038R190311T210538ZChthonyx
007APL Dyalog Unicode190302T082124Zvoidhawk
016Pari/GP190302T083647Zalephalp
007Brachylog190302T030524ZUnrelate
007Japt180524T203129ZEtheryte
03112basic180524T202044Z12Me21
040R180524T190824ZJayCe
037PowerShell171011T202746ZAdmBorkB
044Java 8171012T075945ZKevin Cr
031R171011T212222ZGiuseppe
012Perl 6171013T213246ZSean
031Clojure171012T220731ZGabe Lau
131Batch171012T210042ZSlapparo
035Bash171012T124046ZNahuel F
042Excel171012T123004Zpajonk
nanPerl 5171012T114703ZNahuel F
00505AB1E171011T203547ZArnauld
078C GCC171012T024849Zpizzapan
3518C# .NET Core171012T103345ZIan H.
011Retina171011T202036ZMartin E
046PHP171012T092007Zroberto0
109Batch171012T091130ZNeil
026Octave171012T055250ZStewie G
034Haskell171012T063121ZLaikoni
006Pyth171012T043014ZMr. Xcod
026Julia171011T233833Zeaglgene
006J171011T203345ZFrownyFr
032Ruby171012T010443Zparenpar
025Jq 1.5171011T235839Zjq170727
021Octave / MATLAB with Statistics package / toolbox171011T215450ZLuis Men
036Bash + coreutils171011T223601ZDennis
025Octave171011T203631Zflawr
037Haskell171011T215440ZH.PWiz
00505AB1E171011T214544ZRiley
006MATL171011T210058ZDJMcMayh
00405AB1E171011T212758ZLuis Men
nanPerl 5171011T212436ZXcali
005Jelly171011T201648Zcaird co
004Husk171011T204425ZZgarb
037Haskell171011T205311Zflawr
008Pushy171011T203634ZFlipTack
030JavaScript ES6171011T203912ZShaggy
007Convex171011T203531ZErik the
026Mathematica171011T203353ZZaMoC
007Japt171011T202713ZShaggy
010Jelly171011T201837Zhyperneu
028Python 3171011T201541ZLyricLy

Vyxal, 3 bytes

UǏl

Try it Online!

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?

, 7 chars (23 bytes)

Solution

󷺹⭥⤉≡🃌ᚤ⟞

Note: ☾ uses a custom font, but you can run this code in the Web UI

Diagram

Haskell + hgl, 7 bytes

eL2<xuq

Attempt This Online!

Explanation

No xuq, 10 bytes

xuq makes the challenge a little boring, so I tried without it too.

lEq~<K1<nb

Attempt This Online!

Explanation

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)

Attempt This Online!

Explanation

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.

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
=

Try it!

Uiua, 6 bytes

=1/+¬◰

Try it!

=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=*/#'=:

Try it online!

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?:\

Try it online!

A tacit version of {1=(#x)-#?x}.

Jelly, 4 bytes

QṁƑḊ

Try it online!

Q       Is the input uniquified
 ṁƑ     the same length as
   Ḋ    the input without its first element?

Uiua, 7 bytes

=1-⧻⊃⊝⧻

Try it!

=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)

Try it online!

^
(?=
    .*\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)

Try it online!

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)))

Try it online!

^
(?=
    .*(^|,)(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 = ]

Try it online!

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...

GolfScript, 7 bytes

.,\.&,-

Try it online!

.,\.&,- # 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

Try it online!

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

Try it online!

APL (Dyalog Unicode), 7 bytesSBCS

1=≢-≢∘∪

Try it online!

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

Pari/GP, 16 bytes

a->#a==#Set(a)+1

Try it online!

Brachylog, 7 bytes

dl.&l-₁

Try it online!

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.

Try it online!

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)

Try it online!

Approach is different from the other R solution - and not as golfy...

PowerShell, 40 37 bytes

($args|sort -u).count-eq$args.count-1

Try it online!

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:

Try it here.

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)

Try it online!

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.

Perl 6, 12 bytes

{.Set+1==$_}

Try it online!

Convert to Set, test if the number of elements is less by one.

Clojure, 31 bytes

#(=(count(set %))(-(count %)1))

Try it online!

Does the same as LyricLy's answer

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

Perl 5, 25+1 (-p)=26 bytes

$H{$_}=1}{$\=$.-1==keys%H

TIO

05AB1E, 6 5 bytes

{¥>ΘO

Try it online!

{¥>Θ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

Try it Online!

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

Try it online!

67 byte alternative without Linq:

n=>new System.Collections.Generic.HashSet<int>(n).Count==n.Length-1

Try it online!

Retina, 15 12 11 bytes

Thanks to Neil for saving 1 byte.

D`
Mm2`^$
1

Try it online!

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.

Try it on eval.in!

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

Try it online!

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.

Haskell, 34 bytes

f x=[1|a<-x,b<-x,a==b]==1:1:(1<$x)

Try it online! Based on H.PWiz' answer.

Pyth, 6 bytes

qtlQl{

Verify all the test cases.

7 bytes

q1l.-Q{

Verify all the test cases

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

=&#0,=

= 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?

Try it online!

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.

Try it online!

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).

Try it online!

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).

Try it online!

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

Try it online!

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

Try it online!

Haskell, 37 bytes

f x=sum[1|a<-x,b<-x,a==b]==2+length x

Try it online!

05AB1E, 5 bytes

gIÙg-

Try it online!

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=

Try it online!

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

Try it online!

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

Perl 5, 36 + 1 (-a) = 37 bytes

map$k{$_}++,@F;@a=keys%k;say@a+1==@F

Try it online!

Jelly, 8 5 bytes

QL‘=L

Try it online!

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

Try it online!

Explanation

εṠ-u  Implicit input.
   u  Unique elements.
 Ṡ-   Delete them from input, counting multiplicities.
ε     Is the result a singleton list?

Haskell, 37 bytes

f x=sum[1|0<-(-)<$>x<*>x]==2+length x

Try it online!

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.

Try it online!

JavaScript (ES6), 30 bytes

a=>new Set(a).size==a.length-1

Try it online

Convex, 7 bytes

_Å,)\,=

Try it online!

Mathematica, 26 bytes

(l=Length)@Union@#+1==l@#&

Try it online!

Japt, 7 bytes

â ʶUÊÉ

Try it


Explanation

Remove duplicates (â), get length (Ê) and compare equality () with the length (Ê) of the input (U) minus 1 (É).

Jelly, 10 bytes

ċ@€`QṢ⁼1,2

Try it online!

a longer but different approach

Python 3, 30 28 bytes

lambda m:len({*m})+1==len(m)

Try it online!

{*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.