0

Let's say i have 2 arrays with the same length filled with integers

a = [6,3,5,1]
b = [6,2,5,3]

And i want to compare these arrays and get an output in new (c) array so it would look like this

c = [+,-,+,-]

If there will be 0 matches then my new array would give [-,-,-,-] and vice versa in case of 4 matches [+,+,+,+]

1
  • [+,-,+,-] isn't valid Ruby syntax. What kind of objects should the new array contain – strings, symbols or maybe booleans? Commented Jun 9, 2021 at 10:20

2 Answers 2

1
a.each_with_index.map {|x, i| b[i] == x ? '+' : '-'}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use zip to get pairs from both arrays which can then be converted via map, e.g.:

a.zip(b).map { |i, j| i == j }
#=> [true, false, true, false]

to get "+" and "-" instead you'd use:

a.zip(b).map { |i, j| i == j ? '+' : '-' }
#=> ["+", "-", "+", "-"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.