by visarga · 10y ago · 0 comments
the 'sort' in 'sort | uniq' doesn't know you are going to be throwing away all the duplicate data.
If anyone is wondering, here is an implementation of the python approach i have lying around:
#!/usr/bin/env python2
import sys
from collections import defaultdict
c = defaultdict(int)
for line in sys.stdin:
c[line] += 1
top = sorted(c.items(), key=lambda (k,v): v)
for k, v in top:
print v, k, #!/usr/bin/env python2
import sys
from collections import Counter
for pair in Counter(sys.stdin).most_common():
print pair awk -e '!a[$0]++'
This also preserves the original input order, which is a nice property.In fact, that's exactly how Rust's standard library hashset is implemented since rust supports zero-sized types "in userland" (and unit `()` is a ZST):
pub struct HashSet<T, S = RandomState> {
map: HashMap<T, (), S>
}
http://doc.rust-lang.org/src/std/collections/hash/set.rs.htm...