# For 2 arrays, subtract every element in one array from the position-matching element in the other [closed]

For 2 arrays each with X elements, what is the SHORTEST code to subtract every element in the second from every element in the first, 1 to 1

so, for example:

A1 = [10,11,12]

A2 = [1,2,3]

A1 "-" A2 should == [9,9,9]

• Come on. Code golf should have enough complexity for the competitors to exercise some creativity. Really. Apr 13 '11 at 18:47
• dmckee: Exactly my thoughts. This one is way too trivial to make a good task (see the R solution, for example). Now if only people could stop by the Sandbox on meta before posting a task ...
– Joey
Apr 13 '11 at 19:23
• Though I agree that this question is lousy for golf I find it very unfair that it has at the same time a negative vote count but 5 questions of +14. Apr 14 '11 at 14:35
• GP, GAP, J, Fortran, Maxima, ... solution: "A1 - A2" Apr 14 '11 at 14:39

## R, 3 chars

(following Chris' counting method that includes the variable names in the count)

a-b


a and b can be defined as follows:

a <- c(10,11,12)
b <- c(1,2,3)


(c is the array construction function in R)

• Is this the normal way for defining arrays in R? and if not, what would the code be, if you have a and b already defined. Apr 13 '11 at 18:23
• @Samer, yes that's the normal way. The code would be exactly the same. In fact, you can even do c(10,11,12)-c(1,2,3). So theoretically it's only a single character.
– Joey
Apr 13 '11 at 19:03

# J, 1 char

I will answer too because finally I don't have to use 2 bytes worth of enclosing brackets for J. :)

   -

10 11 12 - 1 2 3
9 9 9


## Scheme, 11 chars

Assume the lists are named a and b:

(map - a b)


Example:

(map - '(10 11 12) '(1 2 3))  ; => (9 9 9)


zipWith(-)


will do the job. Please note, that the lists will be truncated, if their length isn't equal.

• Well, the task states that they are of equal length x, so I guess that can be neglected.
– Joey
Apr 13 '11 at 18:22
• Lists? Doesn't Haskell have Arrays? Apr 21 '11 at 2:56
• @user unknown: They exists, but aren't used very often. In this case, using arrays would blow up my solution by about 30 chars. Apr 21 '11 at 10:49
• :) That's why I'm asking. :) Apr 21 '11 at 14:03
>>> class L(list):
...  def __sub__(self, other):
...   return map(int.__sub__,self, other)
...
>>> a=L([10,11,12])
>>> b=L([1,2,3])
>>> a-b
[9, 9, 9]
>>>


# ES6, 20

a.map((x,i)=>x-b[i])


### In Python (24):

map(lambda a,b:a-b,a,b)


### Smaller (21):

map(int.__sub__,a,b)


Groovy:

List a = [1,2,3]
List b=[2,7,9]
[0,1,2].each { println(a[it]-b[it]) }


### Scala, 51 chars

Array(10,11,12) zip Array(1,2,3)map(x=>x._1-x._2)


(but includes initialisation, Array-Declaration)

(10 to 12).zip (1 to 3)map(x=>x._1-x._2)


is a bit shorter, but produces an Vector.

a zip b.map(x=>x._1-x._2)


is even shorter, but of course I could do

a zip b.map f


then. Or just

g


# CJam, 10 bytes

CJam is newer than this challenge, but I thought it's funny how uncompetitive it is for once:

l~]z{~-}%p


This is an STDIN to STDOUT program, reading input like [10 9 8] [1 2 3]. I know everyone else is answering in snippets, but that wouldn't really be shorter here, because you'd have to replace ] by something like a\a\+.

# TI-BASIC, 5 bytes

L1-L2


Input lists are stored in the L1 and L2 tokens. (TI-BASIC doesn't have STDIN or equivalent.)
Answer is stored in Ans.

Example:

{10,11,12→L1
{10 11 12}
{1,2,3→L2
{1 2 3}
L1-L2
{9 9 9}


Not counting USE: syntax math sequences ; ! because it's not needed (on the REPL).

Factor: [ - ] 2map

• And... what language should that be? Apr 21 '11 at 10:49

Not the shortest, but this is what I came up with in javascript...

t=0;
ar=[];
for(i=0;i<ar.length;i++){
t=a[i]-b[i];
ar.push(temp);
};


Input: a = [10,11,12], b = [1,2,3]
Output: [9,9,9]

• You can shorten your code by removing unnecessary whitespace, using one-character variable names, and dropping the var keyword when creating a variable. Feb 1 '15 at 9:05
• You're welcome! Actually, you don't need newlines and indentation in JavaScript, so you can save some more characters if you remove some whitespace and if you use c instead of ar. Also, please include the character count in your answer. Feb 2 '15 at 16:57

# 8th, 10 bytes

Code

' n:- a:op


Explanation

a:op takes two arrays and invokes the word n:- to the corresponding elements of "a1" and "a2", producing a new array "a3".

Example

ok> [10,11,12] [1,2,3] ' n:- a:op .
[9,9,9]

• Aug 25 '17 at 11:50
• @Shaggy - Thank for your comment. A new rule to learn. Aug 25 '17 at 12:01

# Java 8, 45 bytes

a->b->{for(int i=0;i<a.length;a[i]-=b[i++]);}


Modifies the first input-array instead of returning a new one to save bytes.

Explanation:

Try it here.

a->b->{            // Method with two integer-array parameters and no return-type
for(int i=0;     //  Start at index 0
i<a.length;  //  Loop over the input arrays by index
a[i]-=b[i++]   //   Modify array a by subtracting the values of b on the same index
);               //  End of loop
}                  // End of method


# Java 8, 29 bytes

I got creative with I/O types for this. It's a lambda (curried) from an ordered IntStream (A1) to a lambda from a mutable List<Integer> (implementing remove(int), e.g. ArrayList) (A2) to IntStream. Assign to Function<IntStream, Function<List<Integer>, IntStream>>.

s->l->s.map(n->n-l.remove(0))


Try It Online

Yeah, that's right; I'm using stateful lambdas. I could lose 4 byes by taking A2 as a Stack and using pop, but I'm hesitant to stray that far from the List interface.

# Pyt, 3 bytes

←←-


Takes arrays as input; alternatively, if the arrays are already on the stack, with the second one on the top, it's one character:

-


Explanation:

←        Get first array
←       Get second array
-      Get elementwise difference (as an array)


Try it online!

# Funky, 27 bytes

a=>b=>a::map((n i)=>n-b[i])


Try it online!

# Jelly, 1 byte

_


Try it online!

L*,BcB_


Try it online!

Auto vectorisation is so boring.

# Zsh, 27 bytes

That's right, you can zip arrays in your shell scripts!

Input: arrays a and b
Output: stdout

for x y (${a:^b}) <<<$[x-y]


Try it online! (It takes more code to read stdin into arrays than it does to pairwise subtract!)

If the output must be another array, either 27 or 29 bytes.

for x y (${a:^b}) c+=$[x-y]    # if c was declared as an array
for x y (${a:^b}) c+=($[x-y])  # if c was not declared


# SmileBASIC, 13 bytes

ARYOP 1,A,A,B


Inputs: A, B
Output: A

# Japt, 3 bytes

í-V


Try it

í-V     :Implicit input of arrays U & V
í V     :Interleave both
-      :Reduce each pair by subtraction