77
val a: Array[Int] = Array(1,2,4,5)
val b: Array[Int] = Array(1,2,4,5)
a==b // false

Is there a pattern-matching way to see if two arrays (or sequences) are equivalent?

2

6 Answers 6

114

From Programming Scala:

Array(1,2,4,5).sameElements(Array(1,2,4,5))
3
  • 4
    I think this is the correct solution, even though the other one is the accepted. Mar 3, 2016 at 0:45
  • This was the one that helped my FreeSpec test to pass. :-)
    – Norman H
    Feb 14, 2017 at 21:40
  • looks like this method only supports Array of Integer. I tried Array of string( df1.columns.sameElements(df2.columns))) it didnt work Jun 2, 2022 at 8:11
109

You need to change your last line to

a.deep == b.deep

to do a deep comparison of the arrays.

8
  • 20
    This is the canonical way to do it. But just a warning to the performance-hungry: this does create an entire new collection on both sides, so it's not the most efficient possible way to do it.
    – Rex Kerr
    Mar 22, 2011 at 15:26
  • 8
    @Rex yes, it does create a new collection, but this does not mean, that it is inefficient. Look at the implementation of the method deep. It creates a collection, that forwards all calls of the apply method to the original array.
    – E. Verda
    Mar 23, 2011 at 14:54
  • 1
    @E. Verda - Hm, the implementation is not what I'd expected. But it does a pattern match for every element of the array, which is expensive if it's an array of primitives, and for nested arrays it re-wraps the array on every access. If the arrays are almost entirely different it's inexpensive; for matching close arrays, it's going to be expensive compared to a recursive non-constructive solution.
    – Rex Kerr
    Mar 23, 2011 at 15:38
  • @LucaMolteni:do you mean Array.equals? That doesn't seem to provide a deep comparison.
    – mitchus
    May 9, 2016 at 10:58
  • 2
    @matanster deepEquals is deprecated in the latest versions.
    – Johnny
    Apr 12, 2018 at 9:23
22
  a.corresponds(b){_ == _}

Scaladoc: true if both sequences have the same length and p(x, y) is true for all corresponding elements x of this wrapped array and y of that, otherwise false

1
14

For best performance you should use:

java.util.Arrays.equals(a, b)

This is very fast and does not require extra object allocation. Array[T] in scala is the same as Object[] in java. Same story for primitive values like Int which is java int.

1
  • 1
    I ran val t0 = System.nanoTime(); val r = (java.util.Arrays.equals(a,b)) ; val t1 = System.nanoTime(); t1 - t0 on this sample code and very similar code for the other examples ... This option was way faster than the other examples. Mar 21, 2018 at 20:08
5

As of Scala 2.13, the deep equality approach doesn't work and errors out:

val a: Array[Int] = Array(1,2,4,5)
val b: Array[Int] = Array(1,2,4,5)
a.deep == b.deep // error: value deep is not a member of Array[Int]

sameElements still works in Scala 2.13:

a sameElements b // true
1
  • This should be just a comment on the answer.
    – Rok Kralj
    Feb 13, 2021 at 0:54
2

It didn't look like most of the provided examples work with multidimensional arrays. For example

 val expected = Array(Array(3,-1,0,1),Array(2,2,1,-1),Array(1,-1,2,-1),Array(0,-1,3,4))
 val other = Array(Array(3,-1,0,1),Array(2,2,1,-1),Array(1,-1,2,-1),Array(0,-1,3,4))

    assert(other.sameElements(expected))

returns false, throws an assertion failure

deep doesn't seem to be a function defined on Array.

For convenience I imported scalatest matchers and it worked.

import org.scalatest.matchers.should.Matchers._
other should equal(expected)  

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.