Challenges

+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?
Why should this post be closed?

Empty arrays (1 comment)

+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?

+1
−0

# Factor, 46 bytes

USE: math.statistics
IN: h
ALIAS: h histogram

Why does this post require moderator attention?

+1
−0

# Python 3, 3836 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?

+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?

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?

+0
−0

# Ruby, 12 bytes

->{_1.tally}


Attempt it online!

Why does this post require moderator attention?

+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?

+0
−0

# Vyxal, 1 byte

Ċ


Try it Online!

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

Why does this post require moderator attention?

+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?