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

45 Answers 45

1
2
1
\$\begingroup\$

Japt -x, 5 bytes

ü ®¦q

Run it online

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

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!

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

PowerShell, 33 bytes

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

Try it online!

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

Pyth, 8 bytes

lfthTr8S

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

\$\endgroup\$
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\$
2
  • \$\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, 2019 at 10:27
  • \$\begingroup\$ @JoKing Sure, I fixed it. \$\endgroup\$ Feb 25, 2019 at 10:33
0
\$\begingroup\$

Factor, 42 bytes

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

Try it online!

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

Perl 5 -ap, 35 bytes

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

Try it online!

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

Stax, 5 bytes

ëB♀╡╙

Run and debug it

\$\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\$
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\$

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

Python 3, 93 bytes

import collections as c;print(sum(filter(lambda x:x>1,[y-1 for y in c.Counter(i).values()])))

Try it online!

\$\endgroup\$
6
  • 3
    \$\begingroup\$ This is a snippet; you'd need to take input to make a full program or function (you cannot assume it's already in a variable). \$\endgroup\$ Feb 24, 2019 at 21:54
  • \$\begingroup\$ @JonathanAllan I'm also having a hard time figuring out how to answer these questions. In the StackExchange CodeGolf tour, there's no info about this. Also, is tio.run website part of this community..? In addition, is this the correct format \$\endgroup\$
    – DxTx
    Feb 25, 2019 at 7:07
  • 1
    \$\begingroup\$ @DT this or this would be OK; although the code does not perform the task correctly. \$\endgroup\$ Feb 25, 2019 at 7:35
  • 1
    \$\begingroup\$ @DT TIO is not part of StackExchange but it was developed and is maintained by one of the PPCG moderators, Dennis. \$\endgroup\$ Feb 25, 2019 at 7:37
  • \$\begingroup\$ @JonathanAllan, Got it. Thank you. \$\endgroup\$
    – DxTx
    Feb 25, 2019 at 8:19
1
2

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.