20
\$\begingroup\$

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

46 Answers 46

0
\$\begingroup\$

Gaia, 6 bytes

e:uDul

Try it online!

e	| eval as a list
 :	| duplicate
  u	| uniquify
   D	| multiset difference; keep only repeated elements
    u	| uniquify
     l	| find length
\$\endgroup\$
7
\$\begingroup\$

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

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

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.

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

Japt -x, 5 bytes

ü ®¦q

Run it online

\$\endgroup\$
2
\$\begingroup\$

PHP, 39 bytes

a nice occasion to use variable variables:

foreach($argv as$v)$r+=++$$v==2;echo$r;

takes input from command line arguments. Run with -nr or try it online.


$argv[0] is - and that appears only once in the arguments, so it does not affect the result.

\$\endgroup\$
2
\$\begingroup\$

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.
\$\endgroup\$
3
\$\begingroup\$

Haskell, 41 bytes

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.

\$\endgroup\$
0
\$\begingroup\$

Stax, 5 bytes

ëB♀╡╙

Run and debug it

\$\endgroup\$
3
\$\begingroup\$

Haskell, 41 bytes

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.


Haskell, 40 bytes

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

Try it online!

Stealing the method from other answers.

\$\endgroup\$
  • \$\begingroup\$ Dammit, we had the exact same answer \$\endgroup\$ – proud haskeller Feb 28 at 16:29
0
\$\begingroup\$

JavaScript (Node.js), 67 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 5 -ap, 35 bytes

map$k{$_}++,@F;$_=grep$_>1,values%k

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

Factor, 42 bytes

: d ( x -- ) duplicates members length . ;

Try it online!

\$\endgroup\$
5
\$\begingroup\$

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Paul Karam Feb 25 at 6:26
  • 1
    \$\begingroup\$ Or just >1 to keep the 40 bytes count \$\endgroup\$ – Paul Karam Feb 25 at 7:13
  • \$\begingroup\$ @PaulKaram I didn't notice that, thanks! \$\endgroup\$ – Embodiment of Ignorance Feb 25 at 16:20
3
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

Japt, 12 11 9 8 6 bytes

ü èÈÊÉ

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 11 \$\endgroup\$ – ASCII-only Feb 24 at 23:09
  • \$\begingroup\$ 9 9 9? \$\endgroup\$ – ASCII-only Feb 24 at 23:19
  • \$\begingroup\$ 8 8 \$\endgroup\$ – ASCII-only Feb 24 at 23:30
  • 2
    \$\begingroup\$ 6 \$\endgroup\$ – ASCII-only Feb 24 at 23:45
  • 2
    \$\begingroup\$ 6 6 \$\endgroup\$ – ASCII-only Feb 24 at 23:51
0
\$\begingroup\$

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;
\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Jo King Feb 25 at 10:27
  • \$\begingroup\$ @JoKing Sure, I fixed it. \$\endgroup\$ – rexkogitans Feb 25 at 10:33
0
\$\begingroup\$

Pyth, 8 bytes

lfthTr8S

Try it online here, or verify all the test cases at once here.

\$\endgroup\$
1
\$\begingroup\$

MATL, 5 bytes

8#uqz

Try it online! Or verify all test cases.

Explanation

8#u   % Number of ocurrences of each unique value
q     % Subtract 1
z     % Number of nonzeros
\$\endgroup\$
0
\$\begingroup\$

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

arr.GroupBy(x=>x).Where(y=>y.Count()>1).Select(y=>y.Key).Count()

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Hello, and welcome to PPCG! You've answered with a snippet, whereas we only accept functions and programs by default so please adjust to one of those formats. You can also golf your code further by removing the extra spaces (eg: > 1 becomes >1). \$\endgroup\$ – Οurous Feb 25 at 5:43
  • \$\begingroup\$ Hey @Οurous, thank you for informing me. I have updated my answer. Would you mind checking it again... \$\endgroup\$ – DxTx Feb 25 at 7:03
  • \$\begingroup\$ no, input can't be a predefined variable. Surely it wouldn't cost any bytes to turn it into an anonymous function? \$\endgroup\$ – Jo King Feb 25 at 7:21
0
\$\begingroup\$

PowerShell, 33 bytes

($args|group|?{$_.Count-1}).Count

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

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

Python 3, 63 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

C (clang) 175 117 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.

Updates from the comments:

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

original submission

\$\endgroup\$
  • 5
    \$\begingroup\$ Welcome, nice start. I'm not a C person but here's a link to a tips for golfing C page \$\endgroup\$ – MickyT Feb 25 at 0:27
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ – Jo King Feb 25 at 0:38
  • 2
    \$\begingroup\$ @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;} \$\endgroup\$ – ASCII-only Feb 25 at 0:41
  • 1
    \$\begingroup\$ @CollinPhillips yes. as you can see in the link i posted, it still compiles fine without the includes \$\endgroup\$ – ASCII-only Feb 25 at 0:42
  • 2
    \$\begingroup\$ 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);} \$\endgroup\$ – ASCII-only Feb 25 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.