# Compare array (of String) with no regard to the order

**URL:** <https://discourse.julialang.org/t/compare-array-of-string-with-no-regard-to-the-order/62322>\
**Category:** Performance\
**Tags:** array\
**Created:** [June 3, 2021, 7:56am UTC](https://discourse.julialang.org/t/compare-array-of-string-with-no-regard-to-the-order/62322 "2021-06-03T07:56:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laco\_Kovac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laco_kovac/32/20684_2.png) [@Laco\_Kovac](https://discourse.julialang.org/u/Laco_Kovac)\
**Post date:** [June 3, 2021, 7:56am UTC](https://discourse.julialang.org/t/compare-array-of-string-with-no-regard-to-the-order/62322/1 "2021-06-03T07:56:39Z")

</div>

Le’t have 2 arrays (of String, but it doesn’t really matter). I need to compare them, but the order is irrelevant, just that they contain the same Strings/items (and are of the same length, obviously).

i.e. comparison of

x1 = [“a”, “b”, “c”]  
x2 = [“c”, “b”, “a”]

should return true (equality).

What would be the fastest, most efficient way to do that?

Thank you.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [June 3, 2021, 8:06am UTC](https://discourse.julialang.org/t/compare-array-of-string-with-no-regard-to-the-order/62322/2 "2021-06-03T08:06:53Z")

</div>

The fastest way is probably to convert them to `key => count` Dicts and compare those:

```julia
function counter(it)
    y = Dict{eltype(it), Int}()
    for i in it
        y[i] = get(y, i, 0) + 1
    y
end
length(x1) == length(x2) && counter(x1) == counter(x2)

```

If you need to do this comparison multiple times, you can instead keep the arrays sorted, and compare them directly.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [June 3, 2021, 8:20am UTC](https://discourse.julialang.org/t/compare-array-of-string-with-no-regard-to-the-order/62322/3 "2021-06-03T08:20:08Z")

</div>

> [@Laco\_Kovac](#):
>
> x1 = [“a”, “b”, “c”]  
> x2 = [“c”, “b”, “a”]

```julia
x1 = ["a", "b", "c"]
x2 = ["c", "b", "a"]

using StatsBase: countmap
countmap(x1) == countmap(x2)

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [June 3, 2021, 8:35am UTC](https://discourse.julialang.org/t/compare-array-of-string-with-no-regard-to-the-order/62322/4 "2021-06-03T08:35:21Z")

</div>

If you don’t care about duplicates you can use [`issetequal`](https://docs.julialang.org/en/v1/base/collections/#Base.issetequal):

```julia
julia> x1 = ["a", "b", "c"];

julia> x2 = ["c", "b", "a"];

julia> x3 = ["a", "b", "c", "a"]; # duplicates

julia> issetequal(x1, x2)
true

julia> issetequal(x1, x3)
true

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [June 3, 2021, 9:47am UTC](https://discourse.julialang.org/t/compare-array-of-string-with-no-regard-to-the-order/62322/5 "2021-06-03T09:47:10Z")

</div>

> [@fredrikekre](#):
>
> If you don’t care about duplicates

> [@Laco\_Kovac](#):
>
> and are of the same length,

i think he cares

---

<div class="post-metadata">

**Author:** ![Laco\_Kovac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laco_kovac/32/20684_2.png) [@Laco\_Kovac](https://discourse.julialang.org/u/Laco_Kovac)\
**Post date:** [June 3, 2021, 11:28am UTC](https://discourse.julialang.org/t/compare-array-of-string-with-no-regard-to-the-order/62322/6 "2021-06-03T11:28:38Z")

</div>

Thank you, everyone.  
I actually don’t envision in my code any possibility of a duplicate in either array.  
Here are interesting efficiency test results of suggested solutions:

```julia
@time counter(x1) == counter(x2)
  0.000129 seconds (8 allocations: 1.188 KiB)
true

@time countmap(x1) == countmap(x2)
  0.000082 seconds (8 allocations: 1.188 KiB)
true

@time issetequal(x1, x2)
  0.000009 seconds (8 allocations: 960 bytes)
true

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 3, 2021, 1:38pm UTC](https://discourse.julialang.org/t/compare-array-of-string-with-no-regard-to-the-order/62322/7 "2021-06-03T13:38:11Z")

</div>

One thing to consider is if you won’t have duplicates and don’t care about order, you should probably be using a set instead of a vector.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 4, 2021, 7:28am UTC](https://discourse.julialang.org/t/compare-array-of-string-with-no-regard-to-the-order/62322/8 "2021-06-04T07:28:45Z")

</div>

As far as I can tell, the most efficient solution, at least for short collections, is:

```julia
sort(x1) == sort(x2)

```

---

<div class="post-metadata">

**Author:** ![PatrickHaecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrickhaecker/32/222891_2.png) [@PatrickHaecker](https://discourse.julialang.org/u/PatrickHaecker)\
**Post date:** [July 15, 2024, 4:17am UTC](https://discourse.julialang.org/t/compare-array-of-string-with-no-regard-to-the-order/62322/9 "2024-07-15T04:17:04Z")

</div>

> [@DNF](#):
>
> `sort(x1) == sort(x2)`

For everyone having the same problem as in this quite old question:

If order really does not matter, even for the surrounding code, the even faster

```julia
sort!(x1) == sort!(x2)

```

might be an option, which obviously modifies both `x1` and `x2`.
