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

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)

10 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+0
−0

Japt, 8 bytes

ü ®â pZl

Try it

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

0 comment threads

+1
−0

Factor, 46 bytes

USE: math.statistics
IN: h
ALIAS: h histogram
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

J, 8 bytes

~.,:#/.~

Tacit function, this is the de facto method for this problem in J.

Attempt it online!

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

0 comment threads

+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)
+0
−0

Ruby, 12 bytes

->{_1.tally}

Attempt it online!

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

0 comment threads

+0
−0

APL(Dyalog Unicode), 4 bytes SBCS

Anonymous tacit prefix function.

,∘≢⌸

Try it on APLgolf!

 between each unique element and its indices, apply:

,∘≢ concatenate the unique element to the tally of indices

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

0 comment threads

+0
−0

JavaScript (Node.js), 38 32 bytes

-6 bytes thanks to Shaggy!

a=>a.map(v=>d[v]=-~d[v],d={})&&d

Try it online!

Basically just this but returning the result

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

1 comment thread

[32 bytes](https://tio.run/##RY3BCoMwEETP5iv2JAmsUttqDxJ/RHIIxpQWa8QEL2J/PU1SSmHYGR7DzlNu0g7rY3HFbNTo... (1 comment)
+0
−0

Vyxal, 1 byte

Ċ

Try it Online!

Yes, there really is a built-in for this.

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

0 comment threads

+0
−0

Lua, 98 bytes

function(t)r={}for k,v in pairs(t)do r[v]=0 end for k,v in pairs(t)do r[v]=r[v]+1 end return r end

Attempt This 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 (1 comment)
+0
−0

C (gcc), 45, 42 bytes

t[9999];f(s,a)int*a;{for(;s;)t[a[--s]]++;}

Try it online!


Assumptions:

  • Passing the array size to a function can be used as a means to deal with empty arrays (not supported in C).
  • A table of integers can be regarded as a hashtable with an integer value as hash key and the value's frequency as data. Or if you will: a key-value pair.
  • Maximum integer value 9999.
  • The global variable t may have its contents printed outside the function.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »