# Check if two lists are permutations of each other [duplicate]

## Challenge

Given two lists of equal length, find if one of them is a permutation of the other. Output truthy or falsy values, or 1 or 0.

## Test case Examples

{0,1,2,3,4},{2,4,3,0,1} --> True
{2,4,4,7}{2,7,4,6} --> False
{1},{1} --> True


## Other Stuff

• This is code golf, so shortest code wins
• Standard loopholes and I/O methods apply
• Longest test case consists of numbers with at most 16 binary digits and lengths of lists being at most 64.
• I think you should probably have a test case with an element with multiplicity greater than 1. Commented Jul 20 at 0:08
• Will the two lists have equal length? Commented Jul 20 at 4:41
• Suggested test case, assuming lists can have different lengths: {1},{1,1} -- > False Commented Jul 20 at 16:10
• This is a duplicate of Determine whether strings are anagrams. Commented Jul 20 at 16:54
• The tasks are identical. Besides, in many languages a string is just a list of numbers. On this site the criteria for duplicate tasks is ones whose solutions are correct for both tasks or can be trivially modified to the other task. In this case I believe almost every solution to each challenge will work in either. Do not be discouraged that your challenge is a duplicate; people pose duplicate challenges here all the time without noticing. Instead, use the Sandbox next time so you can get feedback on the challenge before posting it. Commented Jul 20 at 18:48

(.g).(==).g
g=sum.map(65^)


Try it online!

27 bytes

g=sum.map(65^)
a%b=g a==g b


Try it online!

Converts the element counts of each list to base 65 by summing 65^n for each element n, and compares the results. Base 65 works because the lists are guaranteed to have 64 elements max. We're also assuming there are no negative values.

Haskell is an interesting language for this challenge because sorting is locked behind a costly import.

35 bytes

import Data.List
a%b=sort a==sort b


Try it online!

# JavaScript (ES6), 27 bytes

Expects (a)(b) and returns a Boolean value.

a=>b=>a.sort()+""==b.sort()


Try it online!

# Vyxal 3, 3 bytes

ᵛS≈


Try it Online!

I haven't golfed in forever.

ᵛS≈ # Takes inputs as a list
S  # Sort
ᵛ   # All elements of the list (both inputs)
≈ # And check if they are equal


# Brachylog, 1 byte

p


Try it online!

Yep, that's just one builtin. Brachylog works in terms of (generally) bidirectional relationships between variables, with two implicit variables conventionally called "input" (?) and "output" (.), but also has a fundamental concept of "declarative failure": here, each of the input and output variables is used to input a list, and the builtin predicate p succeeds in relating them if and only if they're permutations of each other. This is more useful in other contexts, like generating permutations if one of the variables doesn't have a given value, or constraining results of some other computation.

• Nice! I've seen it all now! Commented Jul 20 at 17:54

# NARS2000, 2 bytes

≡⍦


Multiset match: are the two arguments equivalent when treated as multisets? This checks that the elements are both the same and have the same multiplicity.

# Jelly, 2 bytes

œ^


Try it online!

Inverted truthy/falsy (with the non vectorizing logical not monad Ṇ in the footer to demonstrate).

œ^    Symmetric multiset difference.


Ṣ€E (monadic pair input) and œ&Ƒ (dyadic) are both non-inverted for 3 bytes (as well as just œ^Ṇ, come to think of it).

# Python 3.8 (pre-release),  21  31 bytes

+10 bytes due to a bug regarding duplicate items, thanks to chunes.

lambda a,b:sorted(a)==sorted(b)


Try it online!

## Explanation

lambda a,b:                       # Anonymous lambda function
sorted(a)  sorted(b)   # Sort a and b
==            # Check to see if they're equal

• @chunes: It should be fixed now. Commented Jul 20 at 0:45

# Uiua, 6 7 bytes

-1 thanks to ovs

≍∩⊏∩⊸⍏


try it!

   ∩ ⍏ # get grade of both inputs
∩⊏ ⊸  # index both inputs by grade (sort)
≍       # are they equal?

• You can make this a character shorter by sorting using select by rise and then splitting up the both: ≍∩⊏∩⊸⍏
– ovs
Commented Jul 20 at 9:49

=sort(min(n(sort(A:A)=sort(B:B))))


Put the first list in column A1:A, the second list in B1:B, and the formula in cell C1. Outputs 1 for true and 0 for false.

40 bytes:

=join(",",sort(A:A))=join(",",sort(B:B))


Outputs true or false.

# Julia 1.0, 16 bytes

A*B=A∪B==A∩B


Try it online!

Checks if the union and intersections of both lists are equal. A 17-byte solution is possible without Unicode, using the popular sort strategy:

~=sort
A*B=~A==~B


Try it online!

Julia's built-in issetequal would also work if both lists are guaranteed to have equal length.

# JavaScript (Node.js), 39 bytes

(a,b,x=k=>k.sort().join|)=>x(a)==x(b)


Try it online!

• -6 bytes: (a,b)=>a.sort().join,==b.sort() (use single backticks) Commented Jul 19 at 22:20

# Arturo, 16 bytes

\$=>[=sort&sort&]


Try it!

Sort first list, sort second list, check if they are equal. Each time & is used, it refers to the next argument.

# Setanta, 44 bytes

gniomh(a,b){toradh sortail@a()==sortail@b()}


Try on try-setanta.ie

# Wolfram Language (Mathematica), 15 bytes

Try it online!

SameQ@@Sort/@#&


# Charcoal, 11 bytes

⬤⁺θη⁼№θι№ηι


Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - if the two inputs are permutations, nothing if not. Explanation:

  θ         First input
⁺          Plus
η        Second input
⬤           All elements satisfy
№      Count of
ι    Current element
θ     In first input
⁼       Equals
№  Count of
ι Current element
η  In second input
Implicitly print


9 bytes to port @WheatWizard's approach:

⁼ΣＸφθΣＸφη


Try it online! Link is to verbose version of code. Assumes all inputs are non-negative integers and no integer appears more than 999 times and outputs a Charcoal boolean, i.e. - if the two inputs are permutations, nothing if not. Explanation:

   φ        Predefined variable 1000
Ｘ         Vectorised raised to power
θ       First input
Σ          Take the sum
⁼           Equals
φ    Predefined variable 1000
Ｘ     Vectorised raised to power
η   Second input
Σ      Take the sum
Implicitly print


# APL+WIN, 22 bytes

Prompts for the two lists as vectors

(⍴n)=+/n[⍒n←⎕]=m[⍒m←⎕]


Try it online! Thanks to Dyalog Classic

# Japt, 4 bytes

Takes input as a 2D array.

mÍre


Try it

# BQN, 3bytes

Match over sort.

≡○∧


{y[<y]~x@<x}