[Question]
  [
**Task:**
Given an array of numbers as input (you can choose what subset, such as integers or natural numbers), replace all items with the number of times they appear within the array. As an example, `[1, 2, 2, 1, 4, 8, 1]` would become `[3, 2, 2, 3, 1, 1, 3]`.
You can take input/produce output as arrays, lists, tuples, or any other reasonable representation of some numbers.
**Test cases:**
```
[1]                     [1]
[1, 2]                  [1, 1]
[1, 1]                  [2, 2]
[1, 4, 4]               [1, 2, 2]
[4, 4, 2]               [2, 2, 1]
[4, 4, 4, 4]            [4, 4, 4, 4]
[10, 20, 10, 20]        [2, 2, 2, 2]
[1, 2, 2, 4, 4, 4, 4]   [1, 2, 2, 4, 4, 4, 4]
[1, 2, 2, 1, 4, 8, 1]   [3, 2, 2, 3, 1, 1, 3]
```
**Other:**
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), shortest answer (in bytes) per language wins!
  
[Answer]
# [Python 2](https://docs.python.org/2/), 23 bytes
```
lambda l:map(l.count,l)
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUSHHKjexQCNHLzm/NK9EJ0fzf3lGZk6qgqFVQVFmXolCmkZmXkFpiYam5v9ow1iuaEMdBSMIBeWZABGQZQJmGcFZUGFDA6AgEENoqH4wQlIGAA "Python 2 ‚Äì Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), 3 bytes
```
&=s
```
[Try it online!](https://tio.run/##y00syfn/X822@P//aEMdBSMwAjJMdBQsgIxYAA) Or [verify all test cases](https://tio.run/##y00syfmf8F/Ntvi/S8j/aMNYrmhDHQUjCAXlmQARkGUCZhnBWVBhQwOgIBBDaKh@MEJVBhOFGGgBMh0A).
### Explanation
```
     % Implicit input: numeric row vector
&=   % Matrix of all pairwise equality comparisons
s    % Sum of each column
     % Implicit display
```
[Answer]
# JavaScript (ES6), 36 bytes
```
a=>a.map(x=>a.map(y=>t+=x==y,t=0)|t)
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNHNy09J/Z9m@z/R1i5RLzexQKMCxqi0tSvRtq2wta3UKbE10Kwp0fyfnJ9XnJ@TqpeTn66RphFtGKuAATQ1FfT1FYBSXOiKdRSMYnEo1lHAqt4Qh3ojkFFY1JsAUSx287FqMQFrMYrFYQU2V0G0oFoE1YIkheE2A6BxQAyhYzEtwuUjiBSSyag@QpH6DwA "JavaScript (Node.js) ‚Äì Try It Online")
[Answer]
# [APL(Dyalog Unicode)](https://dyalog.com), 6 bytes [SBCS](https://github.com/abrudz/SBCS)
```
+/‚àò.=‚ç®
```
[Try it on APLgolf!](https://razetime.github.io/APLgolf/?h=AwA&c=09Z/1DFDz/ZR7woA&f=S1MwVDACQkMFEwULBUMA&i=AwA&r=tryAPL&l=apl-dyalog&m=dfn&n=f)
```
+/‚àò.=‚ç®  dfn submission
  ‚àò.=   product table using equality
     ‚ç®  applied to the input on the left and the right
+/      reduce by addition / sum
```
[Answer]
# [Factor](https://factorcode.org/), 28 bytes
```
[ dup histogram substitute ]
```
[Try it online!](https://tio.run/##ZU8xDsIwDNz7intB1UYMCB6AWFgQE2IIwdCqtA2xM6Cqbw9pBSgIW7LuztbZvmojvQuH/Xa3WaEh19Edmrk3jFZLlbNoqVnqyK0jkad1dSdgenjqDDHW2ZAhxoAS4xepBKf6IuaHTVj9sLRbFlDFu6a@6n9yVifv5bxrDEdcvEUV7@5vTrdgf44viBfCKbTaIn5lmjy8AA "Factor ‚Äì Try It Online")
It's a bit shorter than the version for squares:
```
[ dup [ '[ _ = ] count ] with map ]
```
## Explanation:
It's a quotation (anonymous function) that takes a sequence from the data stack as input and leaves a sequence on the data stack as output. Assuming `{ 1 4 4 }` is on the data stack when this quotation is called...
* `dup` Duplicate an object.
**Stack:** `{ 1 4 4 } { 1 4 4 }`
* `histogram` Create a histogram from a sequence.
**Stack:** `{ 1 4 4 } H{ { 1 1 } { 4 2 } }`
* `substitute` Take a sequence and an associative array and substitute elements in the sequence that have keys in the assoc with their values.
**Stack:** `{ 1 2 2 }`
[Answer]
# Ruby, 27 bytes
```
f=->*a{a.map{|e|a.count e}}
```
Testing:
```
p f[4,3,4]  #=> [2, 1, 2]
```
[Answer]
# [Raku](http://raku.org/), 10 bytes
```
{.Bag{$_}}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/Ws8pMb1aJb629n9xYqWCkkq8gq2dQnWaAlBISSEtv0jBxtBOB0goGEEoKM9EwQTEAFIQCRADKmZooGBkoAAm7az/AwA "Perl 6 ‚Äì Try It Online")
`.Bag` generates a Bag (a set with multiplicity) from the input argument `$_`. Then `{$_}` slices into that Bag with the original list, producing a list of the multiplicities of the elements of that list, in order.
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 8 bytes
```
‚àã;?{‚àà·µà}·∂ú
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/pfXa6koGunoFRu/6htw6OmpofbFjzc1Vn7cOuE/486uq3tqx91dDzc2lH7cNuc//@jow1jdRSiDXUUjKA0jG8CRCCmCZhphGDCJAwNgMJADKFhpoARksJYAA "Brachylog ‚Äì Try It Online")
[Generates](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods/10753#10753) the output through the output variable.
```
‚àã;          Pair some element of the input with
  ?         the input.
   {  }·∂ú    In how many ways is
    ‚àà·µà      the element an element of the input?
```
Using `·ªç` comes out one byte longer:
# [Brachylog](https://github.com/JCumin/Brachylog), 9 bytes
```
‚ü®·ªç‚ü®‚àãh‚ü©‚àã‚ü©t
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/pfXa6koGunoFRu/6htw6OmpofbFjzc1Vn7cOuE/4/mr3i4uxdIPurozng0fyWQApIl//9HRxvG6ihEG@ooGEFpGN8EiEBMEzDTCMGESRgaAIWBGELDTAEjJIWxAA "Brachylog ‚Äì Try It Online")
Also [generates](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods/10753#10753) the output through the output variable.
```
      ‚àã      Choose an element of the input.
  ‚ü® h‚ü©       It is the first element of
   ‚àã         an element of
‚ü®·ªç     ‚ü©     the list of pairs [element of input, how many times it occurs in input]
        t    the last element of which is the output.
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 2 bytes
```
ƒã‚±Æ
```
[Try it online!](https://tio.run/##y0rNyan8//9I96ON6/4fbn/UtObopIc7ZwDpyP//o6MNY3W4og11FIygNIxvAkQgpgmYaYRgwiQMDYDCQAyhYaaAEYrCWAA "Jelly ‚Äì Try It Online")
## How it works
```
ƒã‚±Æ - Main link. Takes a list L on the left
 ‚±Æ - For each element in L
ƒã  -   Count the times it appears in L
```
[Answer]
# [Python 3](https://docs.python.org/3/), 26 bytes
```
lambda l:[*map(l.count,l)]
```
[Try it online!](https://tio.run/##bU7dDoIgFL7Wpzh3QjGn5pVbb9EdcUGlkw2RGdR6ekJx60cY7Ox8v@iX6Ud1cB0c4ewkHy43DrKhu4FrJPPraJUhEjP37IVs4TTZtkkT4dXtg0sklLYG4fyupTAoYxmmBdvPM030JJRBgkCHBMaOlgxix@MpLQlULMYRWOmYm1azbaFrf1nEvSrqRbGpWAJCRVBsYr5xX1R4vX9hsr@Yz2/C8hsZxd8 "Python 3 ‚Äì Try It Online")
-5 bytes (indirectly) [thanks to xnor](https://codegolf.stackexchange.com/a/224134/66833)!
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 3 bytes
```
/LQ
```
[Test suite](http://pythtemp.herokuapp.com/?code=%2FLQ&test_suite=1&test_suite_input=%5B1%5D%0A%5B1%2C+2%5D%0A%5B1%2C+1%5D%0A%5B1%2C+4%2C+4%5D%0A%5B4%2C+4%2C+2%5D%0A%5B4%2C+4%2C+4%2C+4%5D%0A%5B10%2C+20%2C+10%2C+20%5D%0A%5B1%2C+2%2C+2%2C+4%2C+4%2C+4%2C+4%5D&debug=0)
Explanation:
```
/LQ  | Full program
/LQQ | with implicit variables
-----+-------------------------------------
 L Q | replace each element d in input with
/ Q  | the count of d in input
```
[Answer]
# [Vyxal](https://github.com/Lyxal/Vyxal), 2 bytes
```
vO
```
[Try it Online!](http://lyxal.pythonanywhere.com?flags=&code=vO&inputs=%5B1%2C%202%2C%202%2C%201%2C%204%2C%208%2C%201%5D&header=&footer=)
Wow y'all using unicode in your golfing languages while I'm chilling in the ASCII zone.
## Explained
```
vO  # vectorise count over the input
    # essentially, [input.count(n) for n in input]
```
[Answer]
# JavaScript (ES2021), 35 bytes (not quite reasonable I/O)
```
a=>a.map(n=>a[++(a[n]||=[0])[0],n])
```
Accept an array of ~~1 element array of~~ *negative* numbers. Return an array of 1 element array...
```
f([-1, -2, -2, -1, -4, -8, -1]) // [[3], [2], [2], [3], [1], [1], [3]]
f([[-1], [-2], [-2], [-1], [-4], [-8], [-1]]) // [[3], [2], [2], [3], [1], [1], [3]]
```
~~Just consider input / output as column vectors...~~
---
# JavaScript (ES2021), 42 bytes
Add an extra `.flat()` to make it reasonable would cost +7 bytes (42 bytes in total)
```
a=>a.map(n=>a[++(a[n]||=[0])[0],n]).flat()
```
That's too long.
[Answer]
# [Racket](https://racket-lang.org/), 42 bytes
```
(Œª(b)(map(Œª(y)(count(Œª(x)(= x y))b))b))
```
[Try it online!](https://tio.run/##K0pMzk4t@a@ck5iXrlAE5nBppKSmZealKqT91zi3WyNJUyM3sQDEqtTUSM4vzSsBsSs0NWwVKhQqNTWTwOi/JhcXSJ1CmoK6hoahJpcCDGgYKhihctFkTRRMkAWAXFQNIAE0NYYGCkYGCmASzSYgxKYeLAGyygJoO9C1AA "Racket ‚Äì Try It Online")
[Answer]
# [J](http://jsoftware.com/), 6 bytes
```
1#.=/~
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/DZX1bPXr/mtyKXClJmfkK6QpaOgYasLYhgpGCKaJggmCYwhjAkURikwgEK7MQMHIAEIimQiE6OrAgiAbLIAGK8DAfwA "J ‚Äì Try It Online")
# [K (oK)](https://github.com/JohnEarnest/ok), 9 bytes
```
{+/x=\:x}
```
[Try it online!](https://tio.run/##y9bNz/7/P82qWlu/wjbGqqL2f5q6ho6htYKhghGIALNMFEysQQRIyAQCgcIGCkYGEBKsGggRcmAuSKOFgqHmfwA "K (oK) ‚Äì Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 27 bytes
```
f a=[sum[1|y<-a,y==x]|x<-a]
```
[Try it online!](https://tio.run/##bU7bCsIgGL7fU/xEsI3ssNhVze6D3kD@CyFX0ubGZqDQu5vbgk6K8ut39Mr7m6gq50rglPX3mmUPWyw5sZQafBh/RVdzqei5iSCRpEmLZVnzFjSX1apvuUrWNMY4LeaHi9AnqUQEldBgaCf4OZGLxQxn6W7Hjkrj3o4o@Ky2k0onpddRSwyxaQRDj2MZQmh5PGIZgS2GOAIvOuRm28E20rnfGHC/FPmo@KsYA6aKSfEX84n7oo3X@zNN/Il5/2Z6fEcG8Sc "Haskell ‚Äì Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 1 byte
```
¬¢
```
[Try it online!](https://tio.run/##yy9OTMpM/f//0KL//6MNdRSMwAjIMNFRsAAyYgE "05AB1E ‚Äì Try It Online") Beats all other answers.
```
¬¢  # full program
¬¢  # number of times...
   # (implicit) each element in...
   # implicit input...
¬¢  # appears in...
   # implicit input
   # implicit output
```
[Answer]
# [Perl 5](https://www.perl.org/) `-pa`, 22 bytes
```
s/\S+/grep$_==$&,@F/ge
```
[Try it online!](https://tio.run/##K0gtyjH9/79YPyZYWz@9KLVAJd7WVkVNx8FNPz31/39DBSMgNFQwUbBQMPyXX1CSmZ9X/F@3IBEA "Perl 5 ‚Äì Try It Online")
[Answer]
# Excel 17, bytes
```
=COUNTIF(A1#,A1#)
```
Assuming `A1 = { .... }` then this works. It's a longer, less flexible formula if the data in entered in individual cells.
[Answer]
# Java, 58 bytes
```
l->l.stream().map(x->java.util.Collections.frequency(l,x))
```
[Try it online!](https://tio.run/##tZI9a8MwEEB3/wqNUrGPJGQoJPVSKBTayWPpoDqykStLinQOMSW/3ZU/kg7t0IBzSAjdHU8PThU/8KTafXaytsYhqcIdGpQKXqTHTfQr7dEJXsPdH6Wi0TlKo/tiZJsPJXOSK@49eeVSk6@IhJjyHjmG42DkjtShSjN0Updv74S70rOxt4@nCbrtfbbPGkUpXBqTbPC4JFJSPHQqSc@ClEHNLT0m6Y/fo1FKDDAPhRP7Rui8pSo@MtZtLg9mrUdRg2kQbFBCpWkB3FrV0l4BTEGXjEE@wugENc4Dmr6BMsauo8VkNTdwdsN1WLMy1wNzdQPm7KrLRRANezxnn/2wbqV@5o8jvP/3zzhFp@4b)
# Java, 56 bytes
```
l->l.stream().map(x->l.stream().filter(y->y==x).count())
```
This only works for integers in the Integer Cache (-128 to 127, by default).
[Try it online!](https://tio.run/##tZLBasMwDIbveQof7dGYtvQwaJLLYFDoTjmOHTzXCU4d29hKaRh99sxO2sHYDhukQkZYkn99IDfsxNLmcBxka40D1IQ77UAqupcetsmPtAcnWEsffilVneYgjY7FxHbvSnLEFfMevTCp0UeCgl3zHhiEcDLygNpQxSU4qevXN8Rc7cnUG@35KppFnmynQdTCFQtUjhzZ3ui6KFCVDyotbnSY0JZZfP6WqaQC4XCfFn2enwnlptOACRm2X7PK3oNoqemA2kADSuOKMmtVj@N0aiq8IvGlUoIDfpqicZ6CiQ1BjfxPbYHWcwvOTrgJPqvmZtRc30FzdtTVMoCGM8XZdz/6vdBv@tMKH//8My7JZfgE)
[Answer]
# [Thunno](https://github.com/Thunno/Thunno), \$ 2 \log\_{256}(96) \approx \$ 1.65 bytes
```
ec
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72sJKM0Ly9_wYKlpSVpuhaLUpMhDCh_wdZoQx0FIzACMkx0FCyAjFiIJAA)
```
ec : implicit input array
e  : map the input to the following:
 c :   count the occurrences of the number in the input
   : implicit output
```
Thought I'd try an easy and short problem for my first Thunno answer.
[Answer]
# [Julia 1.0](http://julialang.org/), ~~24~~ 21 bytes
```
[[email protected]](/cdn-cgi/l/email-protection)(==(a),[a])
```
[Try it online!](https://tio.run/##bZGxCoMwEIb3PMWBi4KURh26BFx8g25yQyhCW2wtTaROvnp6JhRieuFCjvzf/fkh93m8abk4t2rVHi7T/LS5Urkuyl5j4TLoFv14jYNozXX6wNrLEipf1DQlnKhBITI4D8Ya0WpjhrfdOARmKQWkxBh5IY955x0peXLLk5CUrEHWM4UbD1fI2yYZApyab3CkxEmO5EE7nPhnziQPt7uHouQ7hRuMfiUM1j@l9iJVjcJ9AQ "Julia 1.0 ‚Äì Try It Online")
-3 bytes thanks to MarcMush: replace `Ref(a)` with `[a]`
[Answer]
# [R](https://www.r-project.org/), 26 bytes
```
ave(a,a<-scan(),FUN=table)
```
[Try it online!](https://tio.run/##K/r/P7EsVSNRJ9FGtzg5MU9DU8ct1M@2JDEpJ1Xzv6GCERAaKpgoWCgY/gcA "R ‚Äì Try It Online")
`ave` takes a vector `x`, an arbitrary number of grouping variables `...`, and a function `FUN`, and replaces each element of `x` with the result of applying `FUN` to the group containing that element.
I've also found a number of 26 byte variants with `FUN=sum`; they differ only in the way they generate a vector of ones with length `length(a)`.
```
ave(a^0,a<-scan(),FUN=sum)
ave(a,a<-scan(),FUN=sum)/a
ave(a|1,a<-scan(),FUN=sum)
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 4 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
¬£√®¬•X
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=o%2bilWA&input=WzEsMiwyLDEsMyw0LDQsMl0tUQ)
maps input by returning number of occurrence in input
[Answer]
# [PHP](https://php.net/), 56 bytes
```
fn($a)=>array_map(fn($e)=>array_count_values($a)[$e],$a)
```
[Try it online!](https://tio.run/##dU/NCoMwDL73KXLooYWCU7w5tweRIGWreJgu6Crs6bs62fCnLSmBfH8JteTOV2qJMd5ACa7pBdeyvOhh0O@60yTmiflPbk/bv@pJP6wZZ2bFDSrfXcHYpIf6bjsSvBFVinB4UhaQJOCxHVdBhjGuggA9jdGz2epAz31hxD2gyL@KDGMBx5UWxTbmp1hhu8VO3sv/pWMgJnzNAqxsd9dsMPcB "PHP ‚Äì Try It Online")
As usual, those "array\_" PHP prefixes are ruining the golfing :P
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), 32 bytes
Nothing special
```
a=>a.Select(x=>a.Count(y=>x==y))
```
[Try it online!](https://tio.run/##Sy7WTS7O/O9Wmpdsk5lXEh2ro@Dpmleam1qUmJSTChKys1NIA8oq2Cr8T7S1S9QLTs1JTS7RqACxnfNL80o0Km3tKmxtKzU1/1tzOefnFefnpOqFF2WWpPpk5qVqFJcUZeal63nlZ@ZpKOko6YBN08hLLY@OVahWMFSo1dTUJEOfgY6CERCDaXLNAGoFIyDDREfBQgfmmv8A "C# (Visual C# Interactive Compiler) ‚Äì Try It Online")
[Answer]
# [Neim](https://github.com/okx-code/Neim), 2 bytes
Right language, right time.
```
Œ®ùï†
```
[Try it online!](https://tio.run/##y0vNzP3//9yKD3OnLvj/P9pQwQgIDRVMFCwUDGMB "Neim ‚Äì Try It Online")
Explanation:
```
Œ®  # Apply next token to all in list
 ùï† # Count element in list
```
[Answer]
# [C (clang)](http://clang.llvm.org/), ~~84~~ \$\cdots\$ ~~81~~ 75 bytes
Saved 6 bytes thanks to [AZTECCO](https://codegolf.stackexchange.com/users/84844/aztecco)!!!
```
b;i;j;f(*a,l){for(i=-1;++i<l;printf("%d ",b))for(b=j=l;j--;)b-=a[i]!=a[j];}
```
[Try it online!](https://tio.run/##dZPrboMwDIX/8xQeUiXSglauRUvZHmSrJnqhCsroVKrdEK8@ZpK0UG@roA7@Tk4sy9l4G5lX@65bc8FLXjjT3JWsKQ5HR2Sez2czsZT89SiqU@HYky3Y7pqxHq@zMpO89DzO1l6WP4rVDf6XK952bwexBbXHwRem@fGYf7rQr@WuYlZjAf7QBBQXkMGcY1j2mAOeyaCBy6H1ZGvj7gcb7DsbV8oOz2McWqu1rN7jJReVc3buE6ddfXqeP67Qu/FbPuR8k3MhGKeDS/pKHV7SET5jEmkSKXJlFY8J3ZYYwzluwlfHsWAx1Keev21SotIFpqr66x5IrazF1@5QOLot7NZ8Ts23C2PuE@4THhAeEB4SHhIeER4RHhMeE54QnhC@IHxBeEp4yoamTVXXat003R3XDI6JgYmhiZGJsYmJiQsTU2M@zHu13X2YmVfL5bi6@rq4mqkL0euYstEjPlSLRrpipVnxMQZpKE4BxefrZTOSc04uSPZbCN49jMXFf8Kn6izD69l9bwqZ7@vOe/8B "C (clang) ‚Äì Try It Online")
Inputs a pointer to an array and its length (because array pointers in C don't carry any length info) and prints the occurrence counts.
[Answer]
# [Zsh](https://www.zsh.org/), 23 bytes
```
xargs -I. grep -c . f<f
```
[Try it online!](https://tio.run/##qyrO@G@X9r8isSi9WEHXU08hvSi1QEE3WUFPIc0m7f9/Qy4jIDTkMuGy4DL8DwA "Zsh ‚Äì Try It Online")
[Answer]
# [Pyt](https://github.com/mudkip201/pyt), 2 bytes
```
ƒê…î
```
[Try it online!](https://tio.run/##K6gs@f//yISTU/7/jzbUUTACIyDDREfBAsiIBQA "Pyt ‚Äì Try It Online")
```
ƒê      implicit input; duplicate
 …î     for each item in input, count occurrences in input; implicit print
```
]