Lonely Multiplication

Based on this question from Code Review.

Given precisely three positive integers, return the product of all of the distinct inputs. If none of the inputs are distinct, return 1. That is, implement the function:

$$\ f(a,b,c) = \cases{1 & a = b = c \\ a & b = c \\ b & a = c \\ c & a = b \\ a b c & otherwise } \$$

Shortest code in each language wins.

Test cases

a, b, c -> f(a,b,c)
7, 7, 7 -> 1
3, 3, 5 -> 5
2, 6, 6 -> 2
3, 4, 3 -> 4
1, 2, 3 -> 6
12, 9, 16 -> 1728

• "(a=b)∩(b=c)" - did you mean \wedge instead of \cap? – ngn Feb 11 at 6:54
• I’m pretty sure simply writing a = b = c is more readable. – Fatalize Feb 11 at 8:42
• I wrote it the way I did thinking it would be clearer to an average reader of this site, but really I was probably overthinking it. I've changed it now, thanks for the suggestions! – FryAmTheEggman Feb 11 at 16:18
• @ngn I like \land. same effect, more meaningful (stupid \cap and \cup) – qwr Feb 12 at 3:33

Ruby, 38 bytes

->*a{r=1;a.map{|w|(a-[w])[1]&&r*=w};r}


Try it online!

Whitespace, 178 bytes

[S S S N
_Push_0][S N
S _Dupe_0][T    N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_A][S N
S _Dupe_input_A][S N
S _Dupe_input_A][T  N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_B][S N
S _Dupe_input_B][S N
S _Dupe_input_B][T  N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_C][S N
S _Dupe_input_C][S T    S S T   S N
_Copy_0-based_2nd_input_B][T    S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_C==B][S N
S _Dupe_input_C][S T    S S T   T   N
_Copy_0-based_3rd_input_A][T    S S T   _Subtract][N
T   S T N
_If_0_Jump_to_Label_C!=B&&C==A][S T S S T   S N
_Push_0-based_2nd_input_A][S T  S S T   S N
_Push_0-based_2nd_input_B][T    S S T   _Subtract][N
T   S N
_If_0_Jump_to_Label_PRINT][T    S S N
_Multiply][T    S S N
_Multiply][N
S N
N
_Jump_to_Label_PRINT][N
S S T   N
_Create_Label_C!=B&&C==A][S N
N
S N
N
_Jump_to_Label_PRINT][N
S S S N
_Create_Label_C==B][S N
S _Dupe_input_C][S T    S S T   T   N
_Copy_0-based_3rd_input_A][T    S S T   _Subtract][N
T   S T T   N
_If_0_Jump_to_Label_C==B&&C==A][S N
N
N
S N
N
_Jump_to_Label_PRINT][N
S S T   T   N
_Create_Label_C==B&&C==A][S S S T   N
_Push_1][N
S S N
_Create_Label_PRINT][T  N
S T _Print_as_integer]


Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Whitespace work-flow:

Integer A = STDIN as integer
Integer B = STDIN as integer
Integer C = STDIN as integer                   (the stack now contains [A,B,C])
If(A==B): Jump to Label PRINT                  (top of the stack is here C)
Multiply top two
Multiply top two

Label C!=B&&C==A:
Label C==B:

Label C==B&C==A
Push 1                                       (top of the stack is here 1)
Label PRINT
Print top of the stack as integer to STDOUT


Since the inputs are guaranteed to be positive, it uses heap-address 0 for input A; heap-address A for input B; and heap-address B for input C.

Kotlin, 52 51 43 bytes

filter{z->count{z==it}<2}.fold(1){s,c->s*c}


Try it online!

• Not sure if defining the function in the header like I did is allowed, if it isn't i'll have to go back to the 51 byte answer – Quinn Feb 12 at 19:23

Racket, 74 bytes

λ(n)(apply *(map(λ(x)(if(=(length x)1)(car x)1))(group-by identity n))))


Try it online!

Excel, 60 bytes

=IF(A1=B1,IF(A1=C1,1,C1),IF(B1=C1,A1,IF(A1=C1,B1,A1*B1*C1)))


Does not use features like FILTER, only available to Office Insiders.

SQL, 245 155 bytes

 DECLARE @a INT=2,@b INT=4,@c INT=8;WITH v AS(SELECT x FROM(VALUES(@a),(@b),(@c))t(x) GROUP BY x HAVING COUNT(*)=1)SELECT ISNULL(EXP(SUM(LOG(x))), 1) FROM v


Expanded:

DECLARE @a INT=2,
@b INT=4,
@c INT=8;

WITH v
AS (SELECT x
FROM(VALUES(@a),
(@b),
(@c))t(x)
GROUP BY x
HAVING COUNT(*) = 1)
SELECT ISNULL(EXP(SUM(LOG(x))), 1)
FROM v


The straightforward solution is only 150 .. but it is boring. :)

DECLARE @a INT=2,@b INT=4,@c INT=2;SELECT CASE WHEN @a=@b AND @a=@c THEN 1 WHEN @a=@b THEN @c WHEN @b=@c THEN @a WHEN @a=@c THEN @b ELSE @a*@b*@c END

DECLARE @a INT=2,
@b INT=4,
@c INT=2;

SELECT CASE
WHEN @a = @b
AND @a = @c THEN 1
WHEN @a = @b THEN @c
WHEN @b = @c THEN @a
WHEN @a = @c THEN @b
ELSE @a * @b * @c
END

• I'm not too familiar with SQL golfing, but the DECLARE statement seems out of place. I feel as though it either shouldn't be part of the answer, or what you've written isn't precisely reusable? I could easily be wrong - I'm just stating my current understanding. – FryAmTheEggman Feb 13 at 4:55
• Fair point - I am uncertain as well.. I can "assume" the @a, @b, and @c are the parameters passed into a function whose body is the SELECT statement, but is that correct? I could use some guidance there as well. – Forty3 Feb 14 at 2:59

Stax, 13 bytes

üΩ1↑♂Åò←3♠'W▐


Run and debug it

• Thanks for your post! Unfortunately this doesn't seem to meet the requirements for the case where two of the numbers are the same (i.e. for 5 5 7 this returns 35 when it should return 7). Since I don't understand Stax myself, it would also be great if you could include an explanation of what approach your code is using. – FryAmTheEggman Feb 20 at 18:47