How do I compare two arrays in scala?

44,605

Solution 1

You need to change your last line to

a.deep == b.deep

to do a deep comparison of the arrays.

Solution 2

From Programming Scala:

Array(1,2,4,5).sameElements(Array(1,2,4,5))

Solution 3

  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

Solution 4

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.

Solution 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
Share:
44,605
Phil H
Author by

Phil H

Updated on July 05, 2022

Comments

  • Phil H
    Phil H almost 2 years
    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?

  • Rex Kerr
    Rex Kerr about 13 years
    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.
  • E. Verda
    E. Verda about 13 years
    @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.
  • Rex Kerr
    Rex Kerr about 13 years
    @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.
  • Basilevs
    Basilevs over 12 years
    Array is not a sequence scala-lang.org/api/current/index.html#scala.Array, so this will require some (probably implicit) redirections.
  • Alberto Bonsanto
    Alberto Bonsanto about 8 years
    I think this is the correct solution, even though the other one is the accepted.
  • mitchus
    mitchus about 8 years
    @LucaMolteni:do you mean Array.equals? That doesn't seem to provide a deep comparison.
  • matanster
    matanster almost 8 years
    There is also java.util.Objects.deepEquals
  • Norman H
    Norman H about 7 years
    This was the one that helped my FreeSpec test to pass. :-)
  • codeaperature
    codeaperature about 6 years
    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.
  • Johnny
    Johnny about 6 years
    @matanster deepEquals is deprecated in the latest versions.
  • matanster
    matanster about 6 years
    @stas Good to know. I wonder what else has changed in this regard. Any good Scala libraries for working with deeply nested structures?
  • belka
    belka over 5 years
    if you want to compare arrays, regardless of the order of elements though, you should sort them first. arr1.sorted == arr2.sorted hope this will help.
  • Rok Kralj
    Rok Kralj over 3 years
    This should be just a comment on the answer.
  • Aryan Singh
    Aryan Singh almost 2 years
    looks like this method only supports Array of Integer. I tried Array of string( df1.columns.sameElements(df2.columns))) it didnt work