1

I wanted to find if all emement of below array matched to each other:

val a = Array(1,1,1)
val b = Array(1,0,1)
val c = Array(0,1,1)

here output should be

Array(0,0,1) 

as all the value of a(2),b(2) and c(2) is 1 however for all cases it's 0. Is there any functional way of solving this in Scala?

3
  • 4
    There are several ways to solve this problem, but we are not here to just write the code for you. Please include the code of your attempts to make it work, and we can help you if/when you face problems. Commented May 31, 2019 at 18:53
  • 1
    Will always all arrays will have the same size? Will always be only three arrays? Commented May 31, 2019 at 18:57
  • Yes size is same and total 3 array we have. Commented May 31, 2019 at 19:03

1 Answer 1

10

If the arrays are all the same size, then one approach is to transpose the arrays, then map-and-reduce the result with Java's bitwise AND operator &:

val a = Array(1, 1, 1)
val b = Array(1, 0, 1)
val c = Array(0, 1, 1)

val result = Array(a, b, c).transpose.map(_.reduce(_ & _)) // Array(0, 0, 1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Jeffrey Chung for the code.Its helped me.However to improve functional programming do you have any resources or books to start with.

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.