# Count repetitions of an array

You will receive an array and must return the number of integers that occur more than once.

[234, 2, 12, 234, 5, 10, 1000, 2, 99, 234]


This will return 2, since each of 234 and 2 appear more than once.

[234, 2, 12, 234]
[2, 12, 234, 5, 10, 1000, 2]


The list will never be more than 100k integers long, and the integers inside the list will always be in between -100k and 100k.

Integers should be counted if they occur more than once, so if an integer occurs 3 times then it will still only count as one repeated integer.

Test cases

[1, 10, 16, 4, 8, 10, 9, 19, 2, 15, 18, 19, 10, 9, 17, 15, 19, 5, 13, 20]  = 4
[11, 8, 6, 15, 9, 19, 2, 2, 4, 19, 14, 19, 13, 12, 16, 13, 0, 5, 0, 8]     = 5
[9, 7, 8, 16, 3, 9, 20, 19, 15, 6, 8, 4, 18, 14, 19, 12, 12, 16, 11, 19]   = 5
[10, 17, 17, 7, 2, 18, 7, 13, 3, 10, 1, 5, 15, 4, 6, 0, 19, 4, 17, 0]      = 5
[12, 7, 17, 13, 5, 3, 4, 15, 20, 15, 5, 18, 18, 18, 4, 8, 15, 13, 11, 13]  = 5
[0, 3, 6, 1, 5, 2, 16, 1, 6, 3, 12, 1, 16, 5, 4, 5, 6, 17, 4, 8]           = 6
[11, 19, 2, 3, 11, 15, 19, 8, 2, 12, 12, 20, 13, 18, 1, 11, 19, 7, 11, 2]  = 4
[6, 4, 11, 14, 17, 3, 17, 11, 2, 16, 14, 1, 2, 1, 15, 15, 12, 10, 11, 13]  = 6
[0, 19, 2, 0, 10, 10, 16, 9, 19, 9, 15, 0, 10, 18, 0, 17, 18, 18, 0, 9]    = 5
[1, 19, 17, 17, 0, 2, 14, 10, 10, 12, 5, 14, 16, 7, 15, 15, 18, 11, 17, 7] = 5

• What do you mean by Once it counts the repetition, don't count again? Also, since we want to find the repetition of a specific integer, how would we know which integer to search for if we are not given it? Lastly, the test cases are a bit confusing; which are output and which are input? Commented Feb 24, 2019 at 18:19
• I've edited this to try to make it a bit clearer. Is this what you intended? Also, please put answers in for those test cases. Commented Feb 24, 2019 at 18:21
• I have added some answers to the test cases, sorry if I go them wrong Commented Feb 24, 2019 at 19:20
• I've voted to close this question until you confirm this is what you intended. Commented Feb 24, 2019 at 21:01
• Related (output the non-unique items, instead of the amount of non-unique items). Commented Feb 24, 2019 at 21:38

# Retina 0.8.2, 19 bytes

O.+
m^(.+)(¶\1)+$ Try it online! Link includes test suite which splits each line on commas. Explanation: O.+  Sort equal values together. m^(.+)(¶\1)+$


Count the number of runs of at least two values.

# Clean, 59 54 bytes

import StdEnv,StdLib
$l=sum[1\\[_,_:_]<-group(sort l)]  Try it online! Sorts the list, groups adjacent equal elements, and counts the number with more than 1 item. # Rust, 126 bytes let f=|v:Vec<i32>|{let mut u=v.clone();u.sort();u.dedup();u.iter().filter(|i|v.iter().filter(|n|**n==**i).count()>1).count()};  I give up. This is basically the same as Ruby. There is "another way" creating an array and indexing into it using the values in the input vector, +100000, however the type conversions (as usize / as i32) take up too much space. # Pyth, 10 bytes lf<1/QT.{Q  Probably a way to golf it, I'm quite rusty with pyth... Alternate 10 byte version... lf>lT1.gSk  Try it online! # PowerShell, 33 bytes ($args|group|?{$_.Count-1}).Count  Try it online! # MATL, 5 bytes 8#uqz  ### Explanation 8#u % Number of ocurrences of each unique value q % Subtract 1 z % Number of nonzeros  # Pyth, 8 bytes lfthTr8S  Try it online here, or verify all the test cases at once here. ## PHP (112 Bytes) <?php$c=0;foreach(array_count_values(json_decode(file_get_contents('php://stdin'))) as $b)if($b>1)$c++;echo$c;


The assignment does not make it clear if the input is received via Stdin in exactly given format or as separate parameters in Argv, so here is a variant for argv, 91 Bytes:

<?php array_shift($argv);$c=0;foreach(array_count_values($argv) as$b)if($b>1)$c++;echo $c;  • Yes, you can take input via the command line arguments (as per standard IO formats). Though wouldn't there be problems if the filename was numerical? – Jo King Commented Feb 25, 2019 at 10:27 • @JoKing Sure, I fixed it. Commented Feb 25, 2019 at 10:33 # Factor, 42 bytes : d ( x -- ) duplicates members length . ;  Try it online! ## k, 8 bytes +/1<#:'= reads as: sum (length each group) > 1 +/ is sum (plus over) #:' is length each = is group (ex. =1 2 1 6 7 2 generates 1 2 6 7!(0 2;1 5;,3;,4) (dictionary of unique value and its positions)  Use example (first test case) +/1<#:'=1 10 16 4 8 10 9 19 2 15 18 19 10 9 17 15 19 5 13 20  writes 4 # Perl 5-ap, 35 bytes map$k{$_}++,@F;$_=grep\$_>1,values%k


Try it online!

# Stax, 5 bytes

ëB♀╡╙


Run and debug it

# Japt -x, 5 bytes

ü ®¦q


Run it online

# Husk, 5 bytes

LuṠ-u


Try it online!

L        The number of
u       unique elements of
Ṡ-u    the input with every unique element removed once.


# C (gcc), 71 bytes

f(r,l,t)int*r;{int n[7<<15]={};for(t=0;l;t+=!~n[r[--l]+100000]--);r=t;}


Try it online!

Naive approach. 7<<15=229376.

• 70 bytes Commented Mar 29, 2019 at 18:24

# Vyxal, 5 bytes

UÞ⊍UL


Try it Online!

Typical symmetric difference with the unique values in the list, take the length of the unique values in that list.

# Vyxal, 4 bytes

Ċċ∑t


Try it Online!

Just another 4 byter

## Explained

Ċċ∑t­⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁢⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁣⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌­
Ċ     # ‎⁡[[item, count(item)] for item in input]
ċ    # ‎⁢Each item != 1?
∑   # ‎⁣Summate. Because this is implemented as reduce by addition, it vectorises element-wise when folding.
t  # ‎⁤Get the last item of that (the number of counts that != 1)
💎


Created with the help of Luminespire.

# JavaScript (Node.js), 93 bytes

a=>Object.values(a.reduce((a,c)=>Object.assign(a,{[c]:(a[c]|0)+1}),{})).filter(i=>i>1).length


Try it online!

# JavaScript (Node.js), 67 bytes

a=>[...new Set(a.filter(e=>a.indexOf(e)!=a.lastIndexOf(e)))].length


Try it online!

# APL(NARS), 9 chars, 18 bytes

{≢∪⍵∼⍦∪⍵}


test:

  f←{≢∪⍵∼⍦∪⍵}
f 1 2 2 3 1 1 1 1 1 1
2
f 234, 2, 12, 234, 5, 10, 1000, 2, 99, 234
2
f 1, 10, 16, 4, 8, 10, 9, 19, 2, 15, 18, 19, 10, 9, 17, 15, 19, 5, 13, 20
4
f 11, 8, 6, 15, 9, 19, 2, 2, 4, 19, 14, 19, 13, 12, 16, 13, 0, 5, 0, 8
5