Skip to main content
1 of 3

k4 (21)

  {.Q.A@<<>#:'.Q.a#=_x}
  {.Q.A@<<>#:'.Q.a#=_x}@,/0:`:e
"EITUSALNROMCDPVGQBFHJKWXYZ"

This approach uses the builtin alphabetic constants .Q.a (the lowercase letters) and .Q.A (the uppercase) to do the filtering and translation back to upper. The process:

  • _: convert to lowercase (leaves non-letters untouched)
  • =: group into associative array of characters and their indices
  • .Q.a#: take only lowercase letters (includes empty lists for characters that did not occur)
  • #:': count each value in the array
  • >: get the keys of the array ordered by its values, descending
  • <<: find the position where each item would appear if the list were sorted
  • .Q.A@: apply that list of positions back to the list of uppercase letters

The last two steps are conceptually a bit tricky; they basically allow you to permute one list according to another. See the q documentation for rank for more details.

(Note that if we stop before the translation back to uppercase, we have a correct result, but in lowercase:

  {>#:'.Q.a#=_x}@,/0:`:e
"eitusalnromcdpvgqbfhjkwxyz"

and a code length of 14!)