g | x | w | all
Bytes Lang Time Link
031Juby250508T151858ZJordan
006Uiua231017T064239Zchunes
040POSIX Shell + Utilities221229T225634Zнабиячлэ
036Factor221228T220724Zchunes
005BQN220726T034559ZDLosc
056Python220725T215245ZEric Xue
026Desmos220725T101657Zfad
006Vyxal220725T015123Zemanresu
007Vyxal220723T045843Ztybocopp
015APLDyalog Unicode210204T132542ZKamila S
008Stax210204T074014ZRazetime
057Tcl171021T163435Zsergiol
120SmileBASIC180320T205204Z12Me21
028Perl 6170829T201137Znwellnho
nanPerl 5171027T231113ZXcali
044Jq 1.5170921T062549Zjq170727
041Swift 4170921T055754ZAlexande
004Jelly170828T203626ZAdalynn
nanC# .NET Core170828T142259ZDennis.V
061Scala170830T071523ZLlew Val
065Erlang170830T013209ZJoshRage
028Python with numpy170829T144651Zuser4854
021TIBasic170828T180835ZTimtech
045Python 2170828T120332ZMr. Xcod
044Python 3170828T120717Zovs
029R170828T130615ZGiuseppe
059Kotlin170828T130122ZValentin
042Ruby170829T064851Zymbirtt
059Common Lisp170828T190425ZRenzo
097Rust170828T201637Zjferard
006J170828T222655ZAdalynn
012K170828T223701ZAdalynn
020MY170828T215007ZAdalynn
005Dyalog APL170828T203230ZAdalynn
080Swift170828T201006ZFennelou
007J170828T140455Zcole
074PHP170828T181923ZTitus
045Python 3170828T124216ZLuis Men
040Clojure170828T174204ZMattPutn
043JavaScript ES6170828T121252ZShaggy
013Japt170828T160018ZJustin M
016CJam170828T144724ZErik the
075C170828T133432ZSteadybo
031Mathematica170828T135033Zalephalp
079Swift 3170828T132751ZAnonymou
00505AB1E170828T131819ZErik the
00705AB1E170828T120312ZEmigna
007Pyth170828T131536ZErik the
063Java 8170828T121127ZKevin Cr
034Haskell170828T124912ZCristian
007MATL170828T122246ZLuis Men
011Pyth170828T122934ZMr. Xcod
006Jelly170828T121140ZJonathan
036Octave170828T122015Zuser7378

J-uby, 31 bytes

Accidental port of ymbirtt's Ruby answer.

:zip|:*&:[]%[-[+:+,:max],+:<=>]

Attempt This Online!

Uiua, 6 bytes

×+1⊃=↥

Try it!

×+1⊃=↥
   ⊃=↥  # elementwise max and equality mask
 +1     # increment
×       # multiply

POSIX Shell + Utilities, 40 bytes

paste $@|awk '$0=$1<$2?$2:$1>$2?$1:2*$1'

"Arrays" defined as "text files per POSIX".
Obviously buggy insofar as [0], [0] -> []. Doesn't come up in the validator, so. Append the 2-byte "" sequence to also handle that edge case.

Transcript of test cases:

$ cat cg; echo; wc -c cg
paste $@|awk '$0=$1<$2?$2:$1>$2?$1:2*$1'
40 cg
$ ./cg /dev/null                               /dev/null
$ ./cg <(printf '%s\n' 1 2 3)                  <(printf '%s\n' 1 3 2)
2
3
3
$ ./cg <(printf '%s\n' 1 3 3.2 2.3)            <(printf '%s\n' 3 1 3.2 2.6)
3
3
6.4
2.6
$ ./cg <(printf '%s\n' 1 2 3 4 5 5 7 8 9 10)   <(printf '%s\n' 10 9 8 7 6 5 4 3 2 1)
10
9
8
7
6
10
7
8
9
10
$ ./cg <(printf '%s\n' -3.2 -3.2 -2.4 7 -10.1) <(printf '%s\n' 100 -3.2 2.4 -7 -10.1)
100
-6.4
2.4
7
-20.2

Factor, 36 bytes

[ [ 2dup = [ + ] [ max ] if ] 2map ]

Try it online!

BQN, 6 5 bytes

-1 byte by porting emanresu A's Vyxal answer

⌈×1+=

Anonymous tacit function that takes equal-length lists as left and right arguments. Try it at BQN online!

Explanation

⌈×1+=
    =  Compare the two lists itemwise (1 for equal, 0 for not equal)
  1+   Add 1 to each (2 for equal, 1 for not equal)
⌈       Get the itemwise maximums of the two lists
 ×     Multiply (itemwise)

Original solution

The spec can be implemented very literally in 6 bytes:

=◶⌈‿+¨
      ¨  Apply this function to corresponding pairs of elements from the arguments:
=          Test if the elements are equal (1 if so, 0 if not)
 ◶         Use that result to pick a function from this list and apply it:
  ⌈         If 0 (not equal), then max
   ‿+      If 1 (equal), then add

Python, 57 56 bytes

lambda a,b:[x==y and x+y or max(x,y)for x,y in zip(a,b)]

Generators, 57 56 bytes

lambda a,b:(x==y and x+y or max(x,y)for x,y in zip(a,b))

-1 each by Steffan

Desmos, 26 bytes


f(a,b)=\{a=b:2a,a>b:a,b\}

Leading newline necessary.

Try it on Desmos!

Vyxal, 6 bytes

₌Þ∴=›*

Try it Online!

₌      # Do both of these...
 Þ∴    # Elementwise maximum
   =›  # Equality, incremented -> list of 1s where unequal, 2s where equal
     * # Multiply by that.

Vyxal, 8 7 bytes

Zƛ≈[∑|G

-1 byte thanks to @emanresu A

Add the flag if you want to see the numbers in decimal format.

Try it Online!

Explanation:

Z        # Zip
 ƛ       # On each pair
   [     # If
  ≈      # All are equal
    ∑    # Sum
     |   # Otherwise
      G  # Get the greatest value

APL(Dyalog Unicode), 15 bytes SBCS

(⊣⌈⊢×1+⊣=⊢)/¨,¨

Try it on APLgolf!

Can be golfed as:

((⌈×1+=)/¨,¨)
⌈×1+=

So it ends up being the same as the other APL answer...

Stax, 8 bytes

Ç√'8½B=☺

Run and debug it

Tcl, 57 bytes

proc M A\ B {lmap a $A b $B {expr $a-$b?max($a,$b):2*$a}}

Try it online!

SmileBASIC, 120 bytes

An answer using ONLY the ARYOP function.

DEF B A,B,L
DIM C[L],D%[L]ARYOP 6,C,A,A,B
ARYOP.,A,A,B
ARYOP 1,B,A,C
ARYOP 1,B,C,B
ARYOP 6,D%,B,-1,0ARYOP 4,A,C,D%,A
END

Function is called as B array1 , array2 , length. Output is stored in array1.

Explanation:

'get minimum, sum, and maximum of B and A
ARYOP #AOPCLP,C,A,A,B 'c=clamp(a,a,b) (gets the minimum of a and b)
ARYOP #AOPADD,A,A,B 'a=a+b (get the sum)
ARYOP #AOPSUB,B,A,C 'b=a-c (get the maximum)
'check if min==max
ARYOP #AOPSUB,B,C,B 'b=c-c (min-max, will be negative or 0)
ARYOP #AOPCLP,D%,B,-1,0 'd%=clamp(b,-1,0) (d% is an integer array. 0=equal, -1=not equal)
'add subtract min from sum if elements were not equal
ARYOP #AOPMAD,A,C,D%,A 'a=c*d%+a

This challenge seemed boring at first, but there are actually a lot of different ways to do it. For example:

-calculate the maximum, add the minimum if B==A

-calculate the sum, subtract A if A<B, subtract B if B<A

-b+max(a-b,0)+a*(a==b)

Perl 6, 34 28 bytes

{map {.max*2/set $_},[Z] $_}

Try it online!

Perl 5, 59 + 1 (-p) = 60 bytes

say join$",map$_>($t=$F[$i++])?$_:$_<$t?$t:$_*2,split/ /,<>

Try it online!

Jq 1.5, 49 44 bytes

transpose|map(if.0==.1then add else max end)

Sample input

[[], []]
[[1, 2, 3], [1, 3, 2]]
[[1, 3, 3.2, 2.3], [3, 1, 3.2, 2.6]]
[[1,2,3,4,5,5,7,8,9,10], [10,9,8,7,6,5,4,3,2,1]]
[[-3.2, -3.2, -2.4, 7, -10.1], [100, -3.2, 2.4, -7, -10.1]]

Sample run

$ jq -Mc 'transpose|map(if.0==.1then add else max end)' input
[]
[2,3,3]
[3,3,6.4,2.6]
[10,9,8,7,6,10,7,8,9,10]
[100,-6.4,2.4,7,-20.2]

Character count

$ echo -n 'transpose|map(if.0==.1then add else max end)' | wc -c
      44

Thanks again to Jonathan Frech for helping eliminate 5 bytes!

Swift 4, 41 bytes

{zip($0,$1).map{$0==$1 ?2*$0:max($0,$1)}}

Test cases:

let f: ([Float], [Float]) -> [Float]
    = {zip($0,$1).map{$0==$1 ?2*$0:max($0,$1)}}

let testcases: [(inputA: [Float], inputB: [Float], expected: [Float])] = [
    (
        inputA: [],
        inputB: [],
        expected: []
    ),
    (
        inputA: [1, 2, 3],
        inputB: [1, 3, 2],
        expected: [2, 3, 3]
    ),
    (
        inputA: [1, 3, 3.2, 2.3],
        inputB:  [3, 1, 3.2, 2.6],
        expected: [3, 3, 6.4, 2.6]
    ),
    (
        inputA: [1,2,3,4,5,5,7,8,9,10],
        inputB:  [10,9,8,7,6,5,4,3,2,1],
        expected: [10, 9, 8, 7, 6, 10, 7, 8, 9, 10]
    ),
    (
        inputA: [-3.2, -3.2, -2.4, 7, -10.1],
        inputB:  [100, -3.2, 2.4, -7, -10.1],
        expected: [100, -6.4, 2.4, 7, -20.2]
    ),
]

for (caseNumber, testcase) in testcases.enumerated() {
    let actual = f(testcase.inputA, testcase.inputB)
    assert(actual == testcase.expected,
        "Testcase #\(caseNumber) \((testcase.inputA, testcase.inputB)) failed. Got \(actual), but expected \(testcase.expected)!")
    print("Testcase #\(caseNumber) passed!")
}

Jelly, 4 bytes

=‘×»

Try it online!

This uses the exact same approach as my APL answer, except Jelly has a builtin for adding one to a number!

C# (.NET Core), using Linq 47+18=65 bytes

x=>y=>x.Zip(y,(a,b)=>a>b?a:b>a?b:b+a).ToArray()

Try it online!

C# (.NET Core), 82 bytes

x=>y=>l=>{for(int i=0;i<l;i++)x[i]=x[i]>y[i]?x[i]:y[i]>x[i]?y[i]:y[i]*2;return x;}

Try it online!

Scala, 61 Bytes

x=>y=>x zip y map(v=>if(v._1==v._2)v._1*2 else v._1 max v._2)

The above is a function literal in Scala. Here's an explanation.

x=>y=>         // Function literal taking 2 * Vector[Float], x and y.
x zip y        // Zip x and y into one list of pairs.
map(           // Replace every element in the list via a function.
v=>            // Function literal that takes a pair of floats.
if(v._1==v._2) // If the pair are equal.
v._1*2         // Set the element to the first member of the pair multiplied by 2.
else           // Otherwise.
v._1 max v._2) // Set it to their max.

Erlang, 65 bytes

fun(A,B)->lists:zipwith(fun(C,C)->C+C;(D,E)->max(D,E)end,A,B)end

example:

1> (fun(A,B)->lists:zipwith(fun(C,C)->C+C;(D,E)->max(D,E)end,A,B)end)([1, 3, 3.2, 2.3], [3, 1, 3.2, 2.6] ).
[3,3,6.4,2.6]

just a straightforward anonymous fun

Python with numpy, 28 bytes

lambda a,b:a*(a>=b)+b*(b>=a)

Assumes input is given as two numpy arrays.

TI-Basic, 23 21 bytes

Prompt A,B
(ʟA=ʟB)ʟA+max(ʟA,ʟB

Too bad lists take up two bytes each...

Python 2, 45 bytes

A mix of my initial solution and @ovs'.

lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)

Try it online!

Python 2, 49 bytes

lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]

Try it online!

Python 2, 46 bytes

@ovs suggested this method to save 3 bytes.

lambda*x:[max(a,b)*-~(a==b)for a,b in zip(*x)]

Try it online!


How?

First off, we pair the corresponding elements, by using either * or zip(). That allows us to do our further golfing by working either with a map or a list comprehension.

The cool trick in this answer is this part: max(x,y)*-~(x==y). How does that work? - Well, as most of you already know, Python auto-converts bool values to integers when they are used in arithmetic operations. Hence, (x==y) gets evaluated as 1, if the condition is met. However, if the two values are not equal, it returns 0 instead. Then, the bitwise operation -~ increments the value returned from the bool by 1, giving us either 2 or 1. max(a,b) gives the maximum value of the pair and * multiplies it by the value returned above (so it gets multiplied by 2 only if they are equal, in which case max() returns the value of both).

This is based on the fact that the sum of two equal numbers is in fact either of them doubled, and kind of "abuses" Python's bool class being a subclass of int.

Python 3, 48 46 44 bytes

-2 bytes thanks to @nwellnhof

lambda*a:map(lambda*x:max(x)*2/len({*x}),*a)

Try it online!

R, 31 29 bytes

function(a,b)pmax(a,b)+a*!a-b

pmax takes the parallel maximum of the two (or more) arrays (recycling the shorter as needed).

I was looking at Luis Mendo's comment and obviously I realized the approach could work for R as well. That got me to 30 bytes, but then I started playing around with different ways of getting indices instead to improve my original answer, and stumbled upon !a-b as TRUE where a==b and FALSE otherwise, equivalent to a==b. However, for whatever reason, R doesn't require parentheses around !a-b as it does for a==b, which saved me two bytes.

As mentioned by JDL in the comments, this works because ! (negation) has lower precedence than the binary operator - in R, which is strange.

Try it online! (new version)

Try it online! (original)

Kotlin, 78 75 71 66 65 59 bytes

It's my first attempt, be cool :D

a.zip(b).map{(a,b)->when{b>a->b;a>b->a;else->a*2}}.toList()

TIO doesn't work with this solution (and i don't know why), source code for testing below

fun main(args: Array<String>) {
    bestOfTwo(floatArrayOf(), floatArrayOf()).print()
    bestOfTwo(floatArrayOf(0F), floatArrayOf(0F)).print()
    bestOfTwo(floatArrayOf(1F,2F,3F), floatArrayOf(1F,3F,2F)).print()
    bestOfTwo(floatArrayOf(1F,3F,3.2F,2.3F), floatArrayOf(3F,1F,3.2F,2.6F)).print()
    bestOfTwo(floatArrayOf(1F,2F,3F,4F,5F,5F,7F,8F,9F,10F), floatArrayOf(10F,9F,8F,7F,6F,5F,4F,3F,2F,1F)).print()
    bestOfTwo(floatArrayOf(-3.2F,-3.2F,-2.4F,7F,-10.1F), floatArrayOf(100F,-3.2F,2.4F,-7F,-10.1F)).print()
}


fun bestOfTwo(a :FloatArray, b :FloatArray): List<Float> =
    a.zip(b).map{(a,b)->when{b>a->b;a>b->a;else->a*2}}.toList()


fun List<Float>.print() {
    this.forEach { print("$it, ") }; println("")
}

EDIT:

-3 by replace "a+b[i]" by "a*2"

-4 by replace "mapIndexed" method by "zip" (Thanks to @AnonymousReality Swift solution)

-5 by replace "Math.max" method by when condition

-1 by change when condition order

-6 by change toFloatArray() by toList()

Ruby, 42 bytes

->a,b{a.zip(b).map{|x,y|[x+y,x,y][x<=>y]}}

Try it online!

The spaceship operator is great.

Common Lisp, 60 59 bytes

(mapcar(lambda(x y)(*(max x y)(if(= x y)2 1)))(read)(read))

Try it online!

-1 byte thanks to @Zacharý!

Rust, 107 97 bytes

|a:V,b:V|a.iter().zip(b).map(|(&x,y)|if x==y{x+y}else{x.max(y)}).collect::<V>();
type V=Vec<f32>;

Try it online!

Saved 8 bytes thanks to @mgc

J, 6 bytes

>.*1+=

A direct translation of my APL answer (and thus works the same as my MY and Jelly answers). Any tips are welcome, since I don't really know J.

Try it online!

Comparison with APL

>.*1+=
⌈ ×1+=

K, 12 bytes

{x|y*(1+x=y)}

Exact same strategy as my other answers.

Try it online!

Any golfing tips are welcome, as I don't really know K.

MY, 20 bytes

αωD6ǵ'69ǵ';ƒ⇹(αω=‘×↵

Try it online!

This uses the exact same approach as my APL and Jelly answers, except MY doesn't have some of the builtins:

Dyalog APL, 5 bytes

⌈×1+=

Try it online!

How?

This works because if the numbers are unequal, 1+= will be 1, which when multiplied by the maximum, is the maximum. When the numbers are equal, 1+= will return 2, when that is multiplied by the maximum, we get twice the maximum, or the maximum added to itself.

Swift, 80 bytes

var o = [Int]();for (c, d) in zip(a,b){o.append(c==d ? c*2 : max(c,d))};return o

J, 7 bytes

>.`+@.=

Try it online!

Takes one list as the left argument and the other as the right.

Luckily, equality is a rank zero operation.

Explanation

>.`+@.=
      =  Compare equality pairwise
    @.   If equal
   +       Sum
>.       (Else) take greater value

@. isn't really an if statement, but in this case it functions as one (it indexes into the gerund >.`+ based on the result of its right argument and applies that to the input).

PHP, 74 bytes

function($a,$b){foreach($a as$i=>$p)echo$p*($p>=$q=$b[$i])+$q*($q>=$p),_;}

Prints the results separated by an underscore. Try it online.

Python 3, 49 46 45 bytes

3 bytes removed thanks to @Mr.Xcoder (splat instead of two arguments), and 1 byte thanks to @ovs (map instead of list comprehension)

lambda*x:map(lambda a,b:a*(a>=b)+b*(b>=a),*x)

Try it online!

Clojure, 40 bytes

#(map(fn[x y]((if(= x y)+ max)x y))% %2)

Try it online!

JavaScript (ES6), 53 49 45 43 bytes

a=>b=>a.map((x,y)=>(y=b[y])>x?y:y<x?x:x+y)

Try it

o.innerText=(f=

a=>b=>a.map((x,y)=>(y=b[y])>x?y:y<x?x:x+y)

)(i.value=[1,3,3.2,2.3])(j.value=[3,1,3.2,2.6]);oninput=_=>o.innerText=f(i.value.split`,`.map(eval))(j.value.split`,`.map(eval))
<input id=i><input id=j><pre id=o>


Explanation

a=>b=>

Anonymous function taking the 2 arrays as arguments via parameters a and b, in currying syntax (i.e., call with f(a)(b)

a.map((x,y)=>                      )

Map over the first array, passing each element through a function where x is the current element and y is the current index.

(y=b[y])

Get the element at index y in the second array and assign that as the new value of y.

>x?y

Check if y is greater than x and, if so, return y.

:y<x?x

Else, check if y is less than x and, if so, return x

:x+y

Else, return the sum of x and y. (Multiplying x or y by 2 would also work here, for the same number of bytes.)

Japt, 13 bytes

íV,È¥Y Ä *XwY

Try it online! with the -Q flag to format the output array.

CJam, 16 bytes

{.{a+_:e>\:=)*}}

Try it online!

Oh hey look, my code is cute :=) but a big dude \:=)*

C, 76 75 bytes

Thanks to @Kevin Cruijssen for saving a byte!

f(a,b,n)float*a,*b;{for(;n--;++a,++b)printf("%f ",*a>*b?*a:*b>*a?*b:*a*2);}

Try it online!

Mathematica, 31 bytes

MapThread[If[#==#2,2#,Max@##]&]

Swift 3, 81 79 Bytes

func n(a:[Double],b:[Double]){for(x,y)in zip(a,b){print((x==y) ?x+y:max(x,y))}}

Swift has an interesting property in that an Int isn't directly castable to a Double, so you have to specify any arrays as being arrays of Doubles before passing them to the function.

(e.g.) var k:[Double] = [1,2,3,4,5,5,7,8,9,10]

Edit: -2 bytes thanks to @EriktheOutgolfer

05AB1E, 5 bytes

øεMÃO

Try it online!

-1 thanks to Emigna.

05AB1E, 9 8 7 bytes

Saved a byte as Erik the Outgolfer pointed out that a list of lists is valid input.

øεMsËi·

Try it online!

Explanation

ø          # zip the lists
 ε         # apply to each pair
  M        # get max
   s       # swap the top 2 elements on the stack
    Ëi     # if all elements are equal
      ·    # double the max

Pyth, 7 bytes

ms.MZdC

Try it here.

Java 8, 80 69 67 66 65 64 63 bytes

(a,b,l)->{for(;l-->0;)if(a[l]>=b[l])b[l]=a[l]*(a[l]>b[l]?1:2);}

Modifies the second input-array instead or returning a new float-array to save bytes.

-11 bytes by taking the length as additional integer-input, which is allowed according to the challenge rules.
-5 bytes thanks to @OliverGrégoire (one byte at a time.. xD)
-1 byte indirectly thanks to @Shaggy's JS answer, by using a[l]*2 instead of a[l]+b[l].

Explanation:

Try it here.

(a,b,l)->{          // Method with 2 float-array and integer parameters and no return-type
  for(;l-->0;)      //  Loop over the array
    if(a[l]>=b[l])  //   If the current value in `a` is larger or equal to `b`:
      b[l]=         //   Modify the second input-array:
       a[l]*        //    Use `a` multiplied by:
        (a[l]>b[l]? //     If the current value in `a` is larger than `b`:
          1         //      Multiply by 1
         :          //     Else (`a` is smaller of equal to `b`):
          2)        //      Multiply by 2
                    //  End of loop (implicit / single-line body)
}                   // End of method

Haskell, 34 bytes

x!y|x>y=x|x<y=y|1<2=x+y
zipWith(!)

Try it online.

MATL, 7 bytes

X>tG=s*

Input is a two-row matrix, where each row is one of the arrays.

Try it online!

Explanation

X>   % Implicit input. Maximum of each column
t    % Duplicate
G    % Push input
=    % Is equal? Element-wise with broadcast. Gives a two-row matrix
s    % Sum of each column. Gives a row vector containing 1 and 2
*    % Multiply, element-wise. Implicit display

Pyth, 11 bytes

m*eSdhnd{dC

Try it here!

Pyth, 12 bytes

m*eSdhqhdedC

Try it here!

or

m*eSdh!tl{dC

Try it here!

Jelly, 6 bytes

żSṀE?€

A dyadic link taking a list of numbers on each side and returning the resulting list.

Try it online! or see a test-suite*.

How?

żSṀE?€ - Link: list of numbers L, list of numbers R   e.g. [1,3,3.2,2.3], [3,1,3.2,2.6]
ż      - zip - interleave L & R                       [[1,3],[3,1],[3.2,3.2],[2.3,2.6]]
     € - for each pair:
    ?  - { if:
   E   -   ...condition: equal                          0      0       1         0
 S     -   ...then: sum                                               6.4
  Ṁ    -   ...else: maximum                             3      3                2.6
       - }                                    ... ->   [3     ,3     ,6.4      ,2.6]

An alternative is this monadic link taking a list of the two lists, also 6 bytes:

+»⁼?"/

* I don't think I've ever created a test-suite footer almost three times the byte count of the code before!

Octave, 36 Byte

@(a,b)a.*(a>b)+b.*(b>a)+2.*a.*(a==b)