In Search of a Soulmate

40

2

Given a nonempty finite list of integers, output a truthy value if there are exactly two equal entries and all other entries are distinct, and a falsey value otherwise.

Examples

truthy:
[1,1]
[1,2,1]
[1,6,3,4,4,7,9]

falsey:
[0]
[1,1,1]
[1,1,1,2]
[1,1,2,2]
[2,1,2,1,2]
[1,2,3,4,5]

flawr

Posted 2017-10-11T20:09:40.317

Reputation: 40 560

I suppose we can't assume that the integers will always be less than 10? – Martin Ender – 2017-10-11T20:17:57.713

1Yes except if your language does not support any larger integers. – flawr – 2017-10-11T20:22:19.027

1Can you elaborate what you mean by consistent? – flawr – 2017-10-11T20:40:59.103

Are the truthy and falsey values returned required to be the same regardless of the input, or could different falsey or truthy values be returned? – Emigna – 2017-10-11T21:01:55.440

No they need to be truthy and falsey as defined. – flawr – 2017-10-11T21:02:01.937

33Saw this on the top of HNQ & thought we’d reached the final interpersonal.se question – gntskn – 2017-10-11T23:15:48.570

@gntskn Saw this on the top of SE's homepage and thought the same. – Tamás Sengel – 2017-10-12T08:10:12.200

Allowing more than one soul mates couple would have add a bit of challenge, everyone is just posting code about removing duplicates and check that the length drop to the original length - 1. – Walfrat – 2017-10-12T10:44:51.513

3@Walfrat Post it as your own challenge. Also such feedback is usually appreciated in the sandbox. – flawr – 2017-10-12T14:37:56.747

Are all values non-negative? – FlipTack – 2017-11-12T08:05:11.797

Yes you can assume that. – flawr – 2017-11-12T09:23:14.833

Answers

22

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.

LyricLy

Posted 2017-10-11T20:09:40.317

Reputation: 3 313

Exactly the solution I had, but forgot about the {*m} shortcut instead of set, well golfed! – FlipTack – 2017-10-11T20:29:31.250

27 bytes for the negation. (Falsey when it should be Truthy, etc) – mbomb007 – 2017-10-11T21:02:37.053

3Here's another weird way to do it (also negation): lambda m:~-len(m[len({*m}):]) – mbomb007 – 2017-10-11T21:06:26.167

9

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?

Zgarb

Posted 2017-10-11T20:09:40.317

Reputation: 39 083

7

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'

James

Posted 2017-10-11T20:09:40.317

Reputation: 54 537

1Since s is sum and sum sums along the first non-singleton dimension (columns), and the matrix is symmetric, couldn't this just be s instead of Xs? – Giuseppe – 2017-10-11T21:11:40.247

1@Giuseppe Ah, TIL. Thankyou! – James – 2017-10-11T21:13:43.437

7

Haskell, 34 bytes

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

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

Laikoni

Posted 2017-10-11T20:09:40.317

Reputation: 23 676

6

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)

caird coinheringaahing

Posted 2017-10-11T20:09:40.317

Reputation: 13 702

QL_L would output -1 for truthy and some number less than -1 or 0 for falsey (e.g. [1,6,3,4,4,7,9,9,9] would return -3, while [1,6,3,4,7,9] would return 0). – Jonathan Allan – 2017-10-11T21:01:51.570

@JonathanAllan Oh yeah. I guess the examples I tested it on all happened to return -2. – caird coinheringaahing – 2017-10-11T21:06:44.123

5

JavaScript (ES6), 30 bytes

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

Try it online

Shaggy

Posted 2017-10-11T20:09:40.317

Reputation: 24 623

4

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.

Martin Ender

Posted 2017-10-11T20:09:40.317

Reputation: 184 808

Save a byte by limiting the newline match to 2, so that the input to the third line is always 0, 1, or 2, simplifying the test. (Annoyingly you can't use A`. to count newlines, because it drops the last one.) – Neil – 2017-10-12T09:00:15.980

@Neil Thanks, the limit is a neat idea. I also tried using A\., but the problem is rather that you can't distinguish a single empty line from having no lines at all. Maybe I should consider terminatingAandG` output with a linefeed if there are any lines. Although that should probably be an option since I can imagine that linefeed being annoying in other scenarios. – Martin Ender – 2017-10-12T10:38:24.033

Matching a single empty line is easy - that's just ^$¶. – Neil – 2017-10-12T11:58:15.893

@Neil No, I mean that the output of A is identical regardless of whether it retains a single empty line or discards all lines. – Martin Ender – 2017-10-12T11:59:06.803

Sorry, that's what I meant by "drops the last one" - it returns one fewer empty line, but then you can't distinguish between 0 and 1. – Neil – 2017-10-12T12:02:49.767

4

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!

FrownyFrog

Posted 2017-10-11T20:09:40.317

Reputation: 3 112

I think you can replace .~ with = – H.PWiz – 2017-10-11T23:13:48.177

@H.PWiz Nice, edited. – FrownyFrog – 2017-10-11T23:46:35.390

4

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!

flawr

Posted 2017-10-11T20:09:40.317

Reputation: 40 560

You inspired me to come up with this approach :)

– Stewie Griffin – 2017-10-12T07:06:45.223

2

@StewieGriffin I think the MATL answer used your exact approach:)

– flawr – 2017-10-12T14:27:33.410

4

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!

FlipTack

Posted 2017-10-11T20:09:40.317

Reputation: 13 242

1Wow, haven't seen a Pushy answer in ages! +1 – caird coinheringaahing – 2017-10-11T21:09:01.100

1@cairdcoinheringaahing Pushy will never die. It will only come back stronger. – FlipTack – 2017-10-11T21:16:04.370

3

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.

AdmBorkBork

Posted 2017-10-11T20:09:40.317

Reputation: 41 581

Can you use the get-unique alias 'gu' instead of group to reach the same result? – Sinusoid – 2017-10-12T20:54:08.143

@Sinusoid Yes, we can. Thanks! – AdmBorkBork – 2017-10-13T12:19:47.330

This doesn't work on the second test case 1,2,1 - get-unique only works on pre-sorted input. How about ($args|sort -u).count-eq$args.count-1 which is also 37 but does work for all the test cases, if you call it like f 1 2 1 instead of f 1,2,1 ? – TessellatingHeckler – 2017-10-15T23:48:27.883

@TessellatingHeckler Ah, good catch. All of the testing I did was with pre-sorted input. Thanks! – AdmBorkBork – 2017-10-16T12:55:24.517

3

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.

Giuseppe

Posted 2017-10-11T20:09:40.317

Reputation: 21 077

131 bytes – JAD – 2017-10-12T19:46:03.347

1@JarkoDubbeldam ah, of course. Good to see you again! It's been a while. – Giuseppe – 2017-10-12T19:48:28.353

Been busy with some other stuff, not sure I'm completely back yet. – JAD – 2017-10-12T20:04:29.270

@Giuseppe, I just read this question and immediately thought of your Original Approach.... Very nice! I would have never thought of the scan() approach. – Joseph Wood – 2017-10-12T23:15:24.277

3

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

Luis Mendo

Posted 2017-10-11T20:09:40.317

Reputation: 87 464

3

Ruby, 32 bytes

->(s){s.uniq.length==s.length-1}

parenparen

Posted 2017-10-11T20:09:40.317

Reputation: 31

Welcome to PPCG! Because this is code golf, you need to include the byte count of your program. I've taken the liberty of doing it for you this time. – Pavel – 2017-10-12T01:16:41.017

How about Array#size? – Travis – 2017-10-12T21:31:46.343

You can get down to 26 bytes by using ->s{s.uniq.size==s.size-1} – Conor O'Brien – 2018-05-25T02:35:07.953

3

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!

Ian H.

Posted 2017-10-11T20:09:40.317

Reputation: 2 431

3

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

pajonk

Posted 2017-10-11T20:09:40.317

Reputation: 2 480

2

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

Arnauld

Posted 2017-10-11T20:09:40.317

Reputation: 111 334

1If it is okay to return any falsey value for the falsey cases, you can skip the Θ, as 1 is the only truthy value in 05AB1E. – Emigna – 2017-10-11T21:06:50.243

@Emigna Thanks! I was not sure whether it was approved by the OP, but I guess it is. – Arnauld – 2017-10-11T21:52:39.610

I'm afraid you need to revert to the previous solution as ¢ will not work. It would count [19,4,4,9] as false and [19,9] as true since it finds the 0 in 10. – Emigna – 2017-10-12T06:10:11.610

@Emigna Thanks for spotting that. I think that's fixed. – Arnauld – 2017-10-12T11:42:41.747

{¥_O should be okay as well. – Emigna – 2017-10-12T11:44:42.677

Yes, but now that's Luis' answer.

– Arnauld – 2017-10-12T11:46:04.667

Ah, didn't see that. And Riley took your old answer. Guess you'll have to stay here then unless there is another strategy that can be used. – Emigna – 2017-10-12T11:48:56.183

2

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

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

Try it online!

Xcali

Posted 2017-10-11T20:09:40.317

Reputation: 7 671

1may be golfed @k{@F}++;say@F==1+keys%k – Nahuel Fouilleul – 2017-10-12T12:47:10.817

2

Haskell, 37 bytes

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

Try it online!

H.PWiz

Posted 2017-10-11T20:09:40.317

Reputation: 10 962

2

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.

Luis Mendo

Posted 2017-10-11T20:09:40.317

Reputation: 87 464

2

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.

eaglgenes101

Posted 2017-10-11T20:09:40.317

Reputation: 577

!a=sum(a.==a')==endof(a)+2 saves a few bytes. Try it online! – Dennis – 2017-10-11T23:55:01.770

2

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!

jq170727

Posted 2017-10-11T20:09:40.317

Reputation: 411

2

Pyth, 6 bytes

qtlQl{

Verify all the test cases.

  • l{ - Gets the number of unique elements.

  • tlQ - Gets the length of the input list, decremented.

  • q - Checks equality.

7 bytes

q1l.-Q{

Verify all the test cases

Mr. Xcoder

Posted 2017-10-11T20:09:40.317

Reputation: 39 774

2

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.

Stewie Griffin

Posted 2017-10-11T20:09:40.317

Reputation: 43 471

2

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!

roberto06

Posted 2017-10-11T20:09:40.317

Reputation: 351

Do you have to use the variable name $argv can you not just use $a instead? – dading84 – 2017-10-12T10:44:56.387

3@dading84 $argv is the list of command line parameters. So: no, he cannot just use $a. – Titus – 2017-10-12T11:36:14.297

2

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

voidhawk

Posted 2017-10-11T20:09:40.317

Reputation: 1 796

1

Jelly, 10 bytes

ċ@€`QṢ⁼1,2

Try it online!

a longer but different approach

HyperNeutrino

Posted 2017-10-11T20:09:40.317

Reputation: 26 575

1

Japt, 7 bytes

â ʶUÊÉ

Try it


Explanation

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

Shaggy

Posted 2017-10-11T20:09:40.317

Reputation: 24 623

Isn't that 12 bytes? Aren't âÊɶ multibyte characters? – RedClover – 2017-10-13T11:54:17.783

1

Haskell, 37 bytes

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

Try it online!

flawr

Posted 2017-10-11T20:09:40.317

Reputation: 40 560

1

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.

Riley

Posted 2017-10-11T20:09:40.317

Reputation: 11 345

1

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!

Dennis

Posted 2017-10-11T20:09:40.317

Reputation: 196 637

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.

pizzapants184

Posted 2017-10-11T20:09:40.317

Reputation: 3 174

return 1==t; can be return!~-t; to save a byte. – Jonathan Frech – 2017-10-12T07:15:12.753

j<i;++j)t+=x[i]==x[j] can be j<i;)t+=x[i]==x[j++] to save a byte. – Kevin Cruijssen – 2017-10-12T08:03:33.563

73 bytes – ceilingcat – 2019-11-02T09:11:20.600

1

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)

Kevin Cruijssen

Posted 2017-10-11T20:09:40.317

Reputation: 67 575

144 bytes: l->l.stream().distinct().count()==l.size()-1 – Nevay – 2017-10-13T20:16:40.490

1

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)

Nahuel Fouilleul

Posted 2017-10-11T20:09:40.317

Reputation: 5 582

1

Clojure, 31 bytes

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

Try it online!

Does the same as LyricLy's answer

Gabe Laughlin

Posted 2017-10-11T20:09:40.317

Reputation: 21

Welcome to PPCG! – Laikoni – 2017-10-12T22:14:02.977

Thanks! Hope this answer is ok, I felt like I was misusing TIO haha – Gabe Laughlin – 2017-10-12T22:17:00.217

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!

Nit

Posted 2017-10-11T20:09:40.317

Reputation: 2 667

1

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.

Unrelated String

Posted 2017-10-11T20:09:40.317

Reputation: 5 300

1

Regex (ECMAScript), 69 bytes

The input is in the form of a comma-delimited list of nonnegative integers in decimal.

^(?=.*(\b\w+\b).*\b\1\b)(?!(.*\b\1\b){3}|.*\b(?!\1\b)(\w+\b).*\b\3\b)

Try it online!

^
(?=
    .*(\b\w+\b)          # \1 = an element that occurs at least twice
    .*\b\1\b             # locate the second occurrence of \1
)
(?!
    (.*\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
    .*\b\3\b             # Assert that \3 does not occur again
)

This can be trivially modified to work on positive integers in unary instead of nonnegative integers in decimal, in -2 bytes (67) by changing \w+ to x+. Making it handle nonnegative integers in unary would be a tad more involved, due to \b not matching between two commas.

Deadcode

Posted 2017-10-11T20:09:40.317

Reputation: 3 022

0

Mathematica, 26 bytes

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

Try it online!

J42161217

Posted 2017-10-11T20:09:40.317

Reputation: 15 931

0

Convex, 7 bytes

_Å,)\,=

Try it online!

Erik the Outgolfer

Posted 2017-10-11T20:09:40.317

Reputation: 38 134

0

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.

Neil

Posted 2017-10-11T20:09:40.317

Reputation: 95 035

0

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

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

TIO

Nahuel Fouilleul

Posted 2017-10-11T20:09:40.317

Reputation: 5 582

0

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

Slapparoo

Posted 2017-10-11T20:09:40.317

Reputation: 1

0

Perl 6, 12 bytes

{.Set+1==$_}

Try it online!

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

Sean

Posted 2017-10-11T20:09:40.317

Reputation: 4 136

0

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

JayCe

Posted 2017-10-11T20:09:40.317

Reputation: 2 655

Nit's Japt answer can (I think) be ported but using unique is certainly close-ish to duplicated – Giuseppe – 2018-05-24T20:41:46.703

0

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.

12Me21

Posted 2017-10-11T20:09:40.317

Reputation: 6 110

0

Pari/GP, 16 bytes

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

Try it online!

alephalpha

Posted 2017-10-11T20:09:40.317

Reputation: 23 988

0

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!

CT Hall

Posted 2017-10-11T20:09:40.317

Reputation: 591

0

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.

Ciaran_McCarthy

Posted 2017-10-11T20:09:40.317

Reputation: 689

0

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)

Jeyanth

Posted 2017-10-11T20:09:40.317

Reputation: 133