40
\$\begingroup\$

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]
\$\endgroup\$
  • \$\begingroup\$ I suppose we can't assume that the integers will always be less than 10? \$\endgroup\$ – Martin Ender Oct 11 '17 at 20:17
  • 1
    \$\begingroup\$ Yes except if your language does not support any larger integers. \$\endgroup\$ – flawr Oct 11 '17 at 20:22
  • 1
    \$\begingroup\$ Can you elaborate what you mean by consistent? \$\endgroup\$ – flawr Oct 11 '17 at 20:40
  • 33
    \$\begingroup\$ Saw this on the top of HNQ & thought we’d reached the final interpersonal.se question \$\endgroup\$ – gntskn Oct 11 '17 at 23:15
  • 3
    \$\begingroup\$ @Walfrat Post it as your own challenge. Also such feedback is usually appreciated in the sandbox. \$\endgroup\$ – flawr Oct 12 '17 at 14:37

50 Answers 50

1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ return 1==t; can be return!~-t; to save a byte. \$\endgroup\$ – Jonathan Frech Oct 12 '17 at 7:15
  • \$\begingroup\$ j<i;++j)t+=x[i]==x[j] can be j<i;)t+=x[i]==x[j++] to save a byte. \$\endgroup\$ – Kevin Cruijssen Oct 12 '17 at 8:03
1
\$\begingroup\$

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)

\$\endgroup\$
1
\$\begingroup\$

Clojure, 31 bytes

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

Try it online!

Does the same as LyricLy's answer

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Laikoni Oct 12 '17 at 22:14
  • \$\begingroup\$ Thanks! Hope this answer is ok, I felt like I was misusing TIO haha \$\endgroup\$ – Gabe Laughlin Oct 12 '17 at 22:17
1
\$\begingroup\$

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)
\$\endgroup\$
  • 1
    \$\begingroup\$ 44 bytes: l->l.stream().distinct().count()==l.size()-1 \$\endgroup\$ – Nevay Oct 13 '17 at 20:16
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

Mathematica, 26 bytes

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

Try it online!

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

Convex, 7 bytes

_Å,)\,=

Try it online!

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

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.

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

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

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

TIO

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

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

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

Perl 6, 12 bytes

{.Set+1==$_}

Try it online!

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

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

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

\$\endgroup\$
  • \$\begingroup\$ Nit's Japt answer can (I think) be ported but using unique is certainly close-ish to duplicated \$\endgroup\$ – Giuseppe May 24 '18 at 20:41
0
\$\begingroup\$

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.

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

Pari/GP, 16 bytes

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

Try it online!

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

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!

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

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.

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

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)
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.