3
\$\begingroup\$

How do I remove all duplicates from list and order it based on count?

s = ["foo", "this", "lem", "foo", "lem"]
s = ["foo", "ebh", "wosv", "wosv", "ebh", "4whcu"]
#should return
>>> ["foo", "lem", "this"]
>>> ["ebh", "wosv", "foo", "4whcu"]

Rules:

  • Must be shorter than the below code in terms of byte size.
  • Order must be based on the count of in descending order.
  • If there are two strings with the same count, the first appearance comes first
  • All duplicates must be removed, only one of each string should be in the final list

I currently have

list({}.fromkeys(sorted(s,key=lambda y:-s.count(y))))

I've noticed answers like set but that arbitrarily changes the order, which I do not want.

Edit: Sorry for the poor quality before. I had whipped this right before I slept and I was quite tired. This is not for StackOverflow because I'm trying to golf/shorten the size of this code as much as possible. I've tried looking for answers but I haven't been able to find anything.

\$\endgroup\$
10
  • \$\begingroup\$ Do you mean "shorter in terms of byte count"? \$\endgroup\$
    – Bubbler
    Aug 19 '20 at 4:17
  • 1
    \$\begingroup\$ Please add more information about the task and what you've tried. See codegolf.meta.stackexchange.com/a/18613/20260 \$\endgroup\$
    – xnor
    Aug 19 '20 at 4:20
  • \$\begingroup\$ This looks more like a stackoverflow question. \$\endgroup\$
    – Razetime
    Aug 19 '20 at 4:49
  • \$\begingroup\$ I've edited the question a bit. \$\endgroup\$ Aug 19 '20 at 16:50
  • 1
    \$\begingroup\$ Does the solution have to be a single expression? Or can we include multiple statements? \$\endgroup\$
    – xnor
    Aug 20 '20 at 10:30
4
\$\begingroup\$

44 bytes

sorted({}.fromkeys(s),key=s.count,reverse=1)

Try it online!

An improvement to the below, replacing the key with a built-in rather than a lambda, and using reverse to swap the comparisons. We'd like to do [::-1] on the final result instead, but that doesn't do the right stable tiebreaks.

We could also use dict(zip(s,s)) in place of {}.fromkeys(s) for the same length.


47 bytes

sorted({}.fromkeys(s),key=lambda y:-s.count(y))

Try it online!

A simple transposition of your code, moving your {}.fromkeys de-duplication trick before the sorting. Since sorting converts to a list, extracting the keys from a dictionary, this saves the list() call of the original.

For ease of reference, the original code is:

53 bytes

list({}.fromkeys(sorted(s,key=lambda y:-s.count(y))))

Try it online!

\$\endgroup\$
9
  • \$\begingroup\$ sorted({}.fromkeys(s),key=s.count,reverse=1) works as well at 44 bytes, since the reverse option doesn't just reverse the final output, but changes the actual sorting. \$\endgroup\$
    – ovs
    Aug 20 '20 at 10:44
  • \$\begingroup\$ @ovs Nice find, was just posting the same \$\endgroup\$
    – xnor
    Aug 20 '20 at 10:45
  • \$\begingroup\$ I'm curious though, why does s.count work in key? \$\endgroup\$ Aug 20 '20 at 20:16
  • \$\begingroup\$ @12944qwerty Consider s.count and lambda x: s.count(x) - I think you will see that they are identical :) \$\endgroup\$ Aug 20 '20 at 22:01
  • \$\begingroup\$ ohh, they both are functions and being called the same way.... \$\endgroup\$ Aug 20 '20 at 22:07
0
\$\begingroup\$

APL, 11 chars

∪⌷⍨∘⊂∘⍒⊢∘≢⌸

⊢∘≢⌸ right arg: number of apperance of each element, in order of first appearance

left arg: unique elements, in order of first appearance`

⌷⍨∘⊂∘⍒ sorts elements of the left arg based on the grade-down of the right arg`

Try it online: https://tio.run/##SyzI0U2pTMzJT///v9jwUdsE9bT8fHUF9ZKMzGIglZOaCyQhQiA2V7ERQk1qUgaQLM8vLkNQEDGT8ozkUnUurjSg4kcdqx71bH/Uu@JRx4xHXU0gsnfSo65FIEbnokc9O7i4HvVNBSpMUyg2RDCN/v8HAA

\$\endgroup\$
2
  • \$\begingroup\$ It’s not quite python. \$\endgroup\$
    – user7467
    Nov 9 '21 at 21:48
  • \$\begingroup\$ Ooops, sorry, I thought it was, like the other challenges, something to be done in the language of choice. \$\endgroup\$
    – Popov
    Nov 10 '21 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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