# 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? – Embodiment of Ignorance Feb 24 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. – Rɪᴋᴇʀ Feb 24 at 18:21
• I have added some answers to the test cases, sorry if I go them wrong – MickyT Feb 24 at 19:20
• I've voted to close this question until you confirm this is what you intended. – Rɪᴋᴇʀ Feb 24 at 21:01
• Related (output the non-unique items, instead of the amount of non-unique items). – Kevin Cruijssen Feb 24 at 21:38

# R, 20 bytes

Is this what you are after? Uses table to count the occurrences of each of the scan input values. Tests if count is > 1 and sums the trues.

sum(table(scan())>1)


Try it online!

• my mind went to duplicated first -- the humble table is so useful for golfing! – Giuseppe Feb 25 at 15:41
• @giuseppe table is a favorite now :) – MickyT Feb 25 at 17:17

f s=sum[1|x<-[-9^6..9^6],filter(==x)s>[x]]


Try it online! Abuses the fact the the integers in the list are guaranteed to be within -100k and 100k.

# Bash + coreutils, 18

sort|uniq -d|wc -l


Try it online!

# APL (Dyalog Unicode), 9 8 bytesSBCS

-1 thanks to ngn

Anonymous tacit prefix function.

+/1<⊢∘≢⌸


Try it online!

+/ sum of

1< whether 1 is less than

⌸ for each unique element:

⊢∘ ignoring the actual unique element,

≢ the count of its occurrences

• {1<≢⍵}⌸ -> 1<⊢∘≢⌸ – ngn Mar 22 at 16:13
• @ngn Thanks. Incorporated. – Adám Mar 27 at 14:11

# C (clang)175117 95 bytes

c(*a,*b){return*a-*b;}r(*l,m){qsort(l,m,4,c);return((!m||l[1]-*l)&l[-1]==*l)+(m?r(l+1,m-1):0);}


Try it online!

This is the first time I've submitted one of these, so let me know if there are any issues with formatting or anything.

• -58 to 117 bytes from Jo King
• -80 to 95 bytes from ASCII-only

original submission

• Welcome, nice start. I'm not a C person but here's a link to a tips for golfing C page – MickyT Feb 25 at 0:27
• 117 bytes => d,i;c(*a,*b){return*a-*b;}r(l[],m){qsort(l,m,4,c);for(i=d=0;++i<m;)d+=((l[i+1]-l[i]||i>m-2)&&l[i-1]==l[i]);return d;}. As @ASCII-only noted, the includes don't affect the compilation of your program – Jo King Feb 25 at 0:38
• @JoKing 100: d;c(*a,*b){return*a-*b;}r(*l,m){qsort(l,m,4,c);for(d=0;~m--;)d+=(!m||l[1]-*l)&l[-1]==*l++;return d;} – ASCII-only Feb 25 at 0:41
• @CollinPhillips yes. as you can see in the link i posted, it still compiles fine without the includes – ASCII-only Feb 25 at 0:42
• 95: c(*a,*b){return*a-*b;}r(*l,m){qsort(l,m,4,c);return((!m||l[1]-*l)&l[-1]==*l)+(m?r(l+1,m-1):0);} – ASCII-only Feb 25 at 1:12

# C# (Visual C# Interactive Compiler), 40 bytes

n=>n.GroupBy(c=>c).Count(c=>c.Count()>1)


The first draft of the spec was unclear, and I thought it mean return all the elements that appear more than once. This is the updated version.

Somehow I didn't notice that my code returned the number of elements that appeared once. Thanks to Paul Karam for catching that!

Try it online!

• Your output is wrong, it needs to count the elements with 2 or more occurences. It should be n=>n.GroupBy(c=>c).Count(c=>c.Count()>=2). The OP says the answer of this list is 2. Your code returns 5. The change I gave you returns 2. – Paul Karam Feb 25 at 6:26
• Or just >1 to keep the 40 bytes count – Paul Karam Feb 25 at 7:13
• @PaulKaram I didn't notice that, thanks! – Embodiment of Ignorance Feb 25 at 16:20

# Python 3, 38 bytes

lambda a:sum(a.count(x)>1for x in{*a})


Try it online!

# J, 11 9 bytes

-2 bytes thanks to Jonah!

1#.1<1#.=


Try it online!

## Original solution:

1#.(1<#)/.~


Try it online!

Explanation:

        /.~   group the list by itself
(   )      for each group
1<#       is the length greater than 1
1#.           sum by base-1 conversion

• Hey Galen. 1#.1<1#.= for 9 bytes + good ol' self-classify fun. – Jonah Feb 24 at 23:15
• @Jonah Thanks! Honestly, I wasn't aware of this. – Galen Ivanov Feb 25 at 7:20
• @Jonah Nice! – Adám Feb 25 at 7:21
• @Adám and here i was pleased that i'd gotten J to tie with APL. Foiled again :) – Jonah Feb 25 at 18:14

# Ruby, 34 bytes

->a{a.uniq.count{|x|a.count(x)>1}}


Try it online!

# 05AB1E, 4 bytes

Ù¢≠O


Try it online! or as a Test Suite

Explanation

   O  # sum
≠   # the false values
¢    # in the count
Ù     # of each unique digit in input

• So all values that are not 1 are false? – Adám Feb 24 at 20:20
• @Adám: Yes, that is correct. – Emigna Feb 24 at 21:01

# Jelly, 4 bytes

ĠITL


Try it online!

...Or ĠIƇL

### How?

ĠITL - Link: list of integers   e.g. [234, 2, 12, 234, 5, 10, 1000, 2, 99, 234]
Ġ    - group indices by value        [[2,8],5,6,3,9,[1,4,10],7]
I   - incremental differences       [[6],[],[],[],[],[3,6],[]]
T  - truthy indices                [1,6]
L - length                        2


IƇ would filter to keep only truthy results of I ([[6],[3,6]]) which also has the desired length.

# Perl 6, 15 bytes

+*.repeated.Set


Try it online!

Pretty self explanatory. An anonymous code block that gets the count (+) of the Set of elements among the repeated elements of the input (*).

I've realised I've posted almost the exact same solution for a related question.

# Python 3, 63 bytes

lambda l:len(C(l)-C({*l}))
from collections import Counter as C


Try it online!

# Java 8, 74 73 bytes

L->L.stream().filter(i->L.indexOf(i)<L.lastIndexOf(i)).distinct().count()


Try it online.

Explanation:

L->                      // Method with ArrayList parameter and integer return-type
L.stream()             //  Create a stream of the input-list
.filter(i->           //  Filter it by:
L.indexOf(i)        //   Where the first index of a value
<L.lastIndexOf(i))  //   is smaller than the last index of a value
.distinct()           //  Deduplicate this filtered list
.count()              //  And return the count of the remaining values


# APL (Dyalog Extended), 8 7 bytesSBCS

Anonymous tacit prefix function using Jonah's method.

+/1<∪⍧⊢


Try it online!

+/ the total number occurrences
literally the sum of Truths

1< where one is less than

∪ the unique elements'

⍧ count in

⊢ the unmodified argument

f(h:t)=sum[1|filter(==h)t==[h]]+f t
f _=0


Try it online!

Count suffixes where the first element h appears exactly once in the part t that comes after.

import Data.List
f l=length$nub$l\\nub l


Try it online!

Stealing the method from other answers.

f[]=0
f(a:s)=sum[1|filter(==a)s==[a]]+f s


This solution basically counts how many elements of the list have the same element appear exactly once later in the list.

f[]=0
f(a:b)|x<-filter(/=a)b,x/=b=1+f x|1>0=f b


Try it online!

This is the naïve approach. There is likely something that could be done to improve this.

f[]=0


We return 0 for the empty list

f(a:b)


In the case of a non-empty list starting with a and then b.

|x<-filter(/=a)b,x/=b=1+f x


If filtering a out of b is different from b (that is a is in b) then we return 1 more than f applied to b with the as filtered out.

|1>0=f b


If filtering as doesn't change b then we just run f across the rest.

Here is another similar approach that has the same length:

f[]=0
f(a:b)|elem a b=1+f(filter(/=a)b)|1>0=f b


Try it online!

# JavaScript (ES6), 40 bytes

a=>a.map(o=x=>n+=(o[x]=-~o[x])==2,n=0)|n


Try it online!

# Wolfram Language 34 bytes

 Length@DeleteCases[Gather@#,{x_}]&


Gather groups identical integers into lists. DeleteCases[...{x_}] eliminates lists containing a single number. Length returns the number of remaining lists (each containing two or more identical integers.

• Count[{_,__}]@*Gather – alephalpha Feb 26 at 3:10

# Japt, 121198 6 bytes

ü èÈÊÉ


With lots of help from @ASCII-Only, and suggestions from @Shaggy and @Luis felipe De jesus Munoz.

Try it online!

• 11 – ASCII-only Feb 24 at 23:09
• – ASCII-only Feb 24 at 23:19
• – ASCII-only Feb 24 at 23:30
• 6 – ASCII-only Feb 24 at 23:45
• – ASCII-only Feb 24 at 23:51

# Pyth, 6 bytes

l{.-Q{


Try it here

### Explanation

l{.-Q{
{Q   Deduplicate the (implicit) input.
.-Q     Remove the first instance of each from the input.
l{        Count unique.


# Brachylog, 7 bytes

ọzt;1xl


Try it online!

Explanation:

ọ          For every unique element E of the input, [E, how many times E occurs]
zt        The last elements of the previous value.
;1x     With every 1 removed,
l    how many there are.


# Element, 40 bytes

_(#'{"2:0+4:'~1+";~2=[''1+""]$2+'[(#]'}  Try it online! This requires input to be in a precise format like [234, 2, 1000, 2, 99, 234] (enclosed with [] with a comma and space between integers). Explanation: _ input (# delete the [ at start of input '{" '} WHILE the string is non-empty '{"2: '} duplicate it '{" 0+ '} add 0 to coerce to integer (gets next number in array) '{" 4: '} make 3 additional copies '{" ' '} temporarily move 1 copy to control stack '{" ~ '} fetch the current map value for given integer '{" 1+ '} increment map value '{" " '} retrieve temporary copy of integer (the key for the map) '{" ; '} store updated map value '{" ~ '} fetch map value again (1 if 1st instance, 2 if 2nd, etc.) '{" 2= '} test for map value = 2, this is the first duplication '{" [ ] '} IF '{" ['' ] '} move stuff from main stack to control stack '{" [ 1+ ] '} increment the counter of duplicate (bottom of stack) '{" [ ""] '} move stuff back to main stack '{"$       '}    take length of current integer
'{"                        2+     '}    add 2 (for the comma and space)
'{"                          '[  ]'}    FOR loop with that number
'{"                          '[(#]'}      trim those many characters from front of input string
$$$$ output result


# 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