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 andp(x, y)
istrue
for all corresponding elementsx
ofthis
wrapped array andy
ofthat
, otherwisefalse
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
Author by
Phil H
Updated on July 05, 2022Comments
-
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 about 13 yearsThis 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 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 theapply
method to the original array. -
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 over 12 yearsArray is not a sequence scala-lang.org/api/current/index.html#scala.Array, so this will require some (probably implicit) redirections.
-
Alberto Bonsanto about 8 yearsI think this is the correct solution, even though the other one is the accepted.
-
mitchus about 8 years@LucaMolteni:do you mean
Array.equals
? That doesn't seem to provide a deep comparison. -
matanster almost 8 yearsThere is also
java.util.Objects.deepEquals
-
Norman H about 7 yearsThis was the one that helped my FreeSpec test to pass. :-)
-
codeaperature about 6 yearsI 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 about 6 years@matanster
deepEquals
is deprecated in the latest versions. -
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 over 5 yearsif 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 over 3 yearsThis should be just a comment on the answer.
-
Aryan Singh almost 2 yearslooks like this method only supports Array of Integer. I tried Array of string( df1.columns.sameElements(df2.columns))) it didnt work