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
Notifications
Mark all as read
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 } }
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Empty arrays (1 comment)

9 answers

+1
−0

J, 8 bytes

~.,:#/.~

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

Attempt it online!

Why does this post require moderator attention?
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
Why does this post require moderator attention?
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!

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

golf (3 comments)
+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!

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

golf (1 comment)
+0
−0

JavaScript (Node.js), 38 bytes

(a,d={})=>(a.map(v=>d[v]=1+d[v]||1),d)

Try it online!

Basically just this but returning the result

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+0
−0

Ruby, 12 bytes

->{_1.tally}

Attempt it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+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.
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+0
−0

Vyxal, 1 byte

Ċ

Try it Online!

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

Why does this post require moderator attention?
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

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!

Like what we're doing? Support us! Donate