4 added 988 characters in body
source | link

APL, 2 bytes

⍋⍋

The “grade up” built-in, applied twice. Works if indexing starts at 0, which isn’t the default for all flavors of APL.

   Try it here!

Why does this work?

⍋x returns a list of indices that would stably sort x. For example:

    x ← 4 4 0 1 1 2 0 1
    ⍋x
2 6 3 4 7 5 0 1

because if you take element 2, then 6, then 3… you get a stably sorted list:

    x[⍋x]
0 0 1 1 1 2 4 4

But the index list that answers this question is subtly different: first we want the index of the smallest element, then the second smallest, etc. — again, keeping the original order.

If we look at ⍋x, though, we see it can give us this list easily: the position of a 0 in ⍋x tells us where the smallest element would end up after sorting, and the position of a 1 in ⍋x tells us where the second smallest element would end up, etc.

But we know ⍋x contains exactly the numbers [0, 1… n−1]. If we grade it again, we’ll just get the index of 0 in ⍋x, then the index of 1 in ⍋x, etc., which is precisely what we’re interested in.

So the answer is ⍋⍋x.

APL, 2 bytes

⍋⍋

The “grade up” built-in, applied twice. Works if indexing starts at 0, which isn’t the default for all flavors of APL.

 Try it here!

APL, 2 bytes

⍋⍋

The “grade up” built-in, applied twice. Works if indexing starts at 0, which isn’t the default for all flavors of APL.  Try it here!

Why does this work?

⍋x returns a list of indices that would stably sort x. For example:

    x ← 4 4 0 1 1 2 0 1
    ⍋x
2 6 3 4 7 5 0 1

because if you take element 2, then 6, then 3… you get a stably sorted list:

    x[⍋x]
0 0 1 1 1 2 4 4

But the index list that answers this question is subtly different: first we want the index of the smallest element, then the second smallest, etc. — again, keeping the original order.

If we look at ⍋x, though, we see it can give us this list easily: the position of a 0 in ⍋x tells us where the smallest element would end up after sorting, and the position of a 1 in ⍋x tells us where the second smallest element would end up, etc.

But we know ⍋x contains exactly the numbers [0, 1… n−1]. If we grade it again, we’ll just get the index of 0 in ⍋x, then the index of 1 in ⍋x, etc., which is precisely what we’re interested in.

So the answer is ⍋⍋x.

3 added 186 characters in body
source | link

APL, 2 bytesbytes

⍋⍋

The “grade up” built-in, applied twice. Works if indexing starts at 0, which isn’t the default for all flavors of APL.

Try it here!

APL, 2 bytes

⍋⍋

The “grade up” built-in, applied twice.

Try it here!

APL, 2 bytes

⍋⍋

The “grade up” built-in, applied twice. Works if indexing starts at 0, which isn’t the default for all flavors of APL.

Try it here!

2 added 8 characters in body
source | link

APL, 1 byte2 bytes

⍋⍋

The “grade up” built-in, applied twice.

Try it here!Try it here!

APL, 1 byte


The “grade up” built-in.

Try it here!

APL, 2 bytes

⍋⍋

The “grade up” built-in, applied twice.

Try it here!

1
source | link