Skip to content

Instantly share code, notes, and snippets.

@ryo1kato
Created May 27, 2021 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryo1kato/1ca0058322628b5a34ac1554281588f7 to your computer and use it in GitHub Desktop.
Save ryo1kato/1ca0058322628b5a34ac1554281588f7 to your computer and use it in GitHub Desktop.
Python Multiset
import collections
class Multiset(collections.Counter):
def add(self, elem):
self[elem] += 1
def remove(self, elem):
self[elem] -= 1
if self[elem] == 0:
del self[elem]
def pop(self):
k, v = self.popitem()
if v > 0:
self[k] = v-1
return k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment