4
\$\begingroup\$

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.
\$\endgroup\$
6
  • 3
    \$\begingroup\$ I think you should probably have a test case with an element with multiplicity greater than 1. \$\endgroup\$ Commented Jul 20 at 0:08
  • 2
    \$\begingroup\$ Will the two lists have equal length? \$\endgroup\$ Commented Jul 20 at 4:41
  • \$\begingroup\$ Suggested test case, assuming lists can have different lengths: {1},{1,1} -- > False \$\endgroup\$ Commented Jul 20 at 16:10
  • 4
    \$\begingroup\$ This is a duplicate of Determine whether strings are anagrams. \$\endgroup\$ Commented Jul 20 at 16:54
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 20 at 18:48

18 Answers 18

4
\$\begingroup\$

Haskell, 26 bytes

(.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!

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Nice! I've seen it all now! \$\endgroup\$ Commented Jul 20 at 17:54
3
\$\begingroup\$

JavaScript (ES6), 27 bytes

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

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

Uiua, 6 7 bytes

-1 thanks to ovs

≍∩⊏∩⊸⍏

try it!

   ∩ ⍏ # get grade of both inputs
 ∩⊏ ⊸  # index both inputs by grade (sort)
≍       # are they equal?
\$\endgroup\$
1
  • \$\begingroup\$ You can make this a character shorter by sorting using select by rise and then splitting up the both: ≍∩⊏∩⊸⍏ \$\endgroup\$
    – ovs
    Commented Jul 20 at 9:49
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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).

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ @chunes: It should be fixed now. \$\endgroup\$ Commented Jul 20 at 0:45
1
\$\begingroup\$

Google Sheets, 34 bytes

=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.

screenshot

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 39 bytes

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ -6 bytes: (a,b)=>a.sort().join``,``==b.sort() (use single backticks) \$\endgroup\$ Commented Jul 19 at 22:20
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Setanta, 44 bytes

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

Try on try-setanta.ie

\$\endgroup\$
0
\$\begingroup\$

Wolfram Language (Mathematica), 15 bytes

Try it online!

SameQ@@Sort/@#&
\$\endgroup\$
0
\$\begingroup\$

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:

⁼ΣXφθΣXφη

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`
  X         Vectorised raised to power
    θ       First input
 Σ          Take the sum
⁼           Equals
       φ    Predefined variable `1000`
      X     Vectorised raised to power
        η   Second input
     Σ      Take the sum
            Implicitly print
\$\endgroup\$
0
\$\begingroup\$

APL+WIN, 22 bytes

Prompts for the two lists as vectors

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

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
0
\$\begingroup\$

Japt, 4 bytes

Takes input as a 2D array.

mÍre

Try it

\$\endgroup\$
0
\$\begingroup\$

BQN, 3bytes

Match over sort.

≡○∧

Try it on BQNPAD!

K (ngn/k), 12 bytes

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

Try it online!

\$\endgroup\$

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