Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on Make a frequency table (histogram)

Parent

Make a frequency table (histogram)

+3
−0

Challenge

Given an array in any suitable format, create a frequency table for it. i.e: Pair each unique element with the number of times it appears in the array.

You can return the frequency table as a list of pairs, hashmap/dictionary, output the pairs directly, etc.

Tests

{ 1 1 2 3 5 6 6 }           -> { { 1 2 } { 2 1 } { 3 1 } { 5 1 } { 6 2 } }
{ }                         -> { }
{ 1 6 7 1 2 6 6 1 53 4 10 } -> { { 1 3 } { 2 1 } { 4 1 } { 53 1 } { 6 3 } { 7 1 } { 10 1 } }
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Empty arrays (1 comment)
Post
+1
−0

Python 3, 38 36 33 bytes

lambda x:{n:x.count(n)for n in x}

-2 bytes thanks to @Razetime

Another -3 bytes thanks to @orthoplex

Attempt it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

golf (3 comments)
golf
Razetime‭ wrote over 2 years ago

set(x){*x}

orthoplex‭ wrote over 2 years ago

Actually, set(x)x works just fine in this case. I also found a set-of-pairs solution that is equally as long: lambda x:{*zip(x,map(x.count,x))}

south‭ wrote over 2 years ago

Ah yes, good point about the dict comp. I had totally forgotten about that behavior. The set-of-pairs is also a great way to think about it. Very impressive!