20
\$\begingroup\$

Given a list of Integers greater than zero, Sort each of the unique values in ascending order, then repeat the process on the remaining duplicate values and append.

Example:

[1,5,2,2,8,3,5,2,9] ==> [1,2,3,5,8,9,2,5,2]
[8,5] ==> [5,8]
[2,2,2] ==> [2,2,2]
\$\endgroup\$
6
  • 13
    \$\begingroup\$ You should add more test inputs, including "corner" cases such as [4 4 4] and [8 5], as well as [] if the empty input need to be handled \$\endgroup\$
    – Luis Mendo
    Dec 12, 2023 at 23:43
  • \$\begingroup\$ Closely related: Simulate Round Robin Scheduling \$\endgroup\$
    – Bubbler
    Dec 13, 2023 at 4:37
  • 1
    \$\begingroup\$ added extra cases \$\endgroup\$
    – pacman256
    Dec 13, 2023 at 15:13
  • 3
    \$\begingroup\$ I read this first as cynically sort a list, and was really curious. Still am, but in a different way. \$\endgroup\$
    – Scot
    Dec 13, 2023 at 23:16
  • 1
    \$\begingroup\$ @DomHastings yeah, i was just lazy typing this out \$\endgroup\$
    – pacman256
    Dec 22, 2023 at 16:30

32 Answers 32

1
2
1
\$\begingroup\$

C# (Visual C# Compiler), 317 bytes

using System.Linq;class P{static void Main() => System.Console.WriteLine(string.Join(", ", S(new ushort[] { 1, 5, 2, 2, 8, 3, 5, 2, 9 }))); static ushort[] S(ushort[] a) => a.Select(v => o[v] = (ushort)(o[v] + 256 | v)).OrderBy(x => x).Select(v => (ushort)(v & 255)).ToArray(); static ushort[] o = new ushort[65536];}

Try it online!

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

Julia 1.0, 56 53 bytes

~a=a>[] ? [sort!(a)[(d=diff([0;a])).>0];~a[d.<1]] : a

Try it online!

sort the array, take each first occurence with diff, and recurse

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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