计算数组的重复次数


20

您将收到一个数组,并且必须返回多次出现的整数数。

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

这将返回2,因为每个2342出现不止一次。

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

该列表的长度永远不会超过100k,并且列表内的整数始终在-100k和100k之间。

如果整数出现多次,则应将其计数,因此,如果整数出现3次,则它仍将仅计为一个重复的整数。

测试用例

[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

你是什么意思Once it counts the repetition, don't count again?另外,由于我们要查找特定整数的重复,如果不给定整数,我们怎么知道要搜索哪个整数?最后,测试用例有些混乱。哪些输出,哪些输入?
无知的体现

4
我对此进行了编辑,以使其更加清晰。这是您想要的吗?另外,请为这些测试用例提供答案。
Rɪᴋᴇʀ

1
我已经为测试用例添加了一些答案,对不起,如果我做错了
MickyT

1
在您确认这是您想要的内容之前,我已投票结束该问题。
Rɪᴋᴇʀ

4
相关(输出非唯一项目,而不是非唯一项目的数量)。
凯文·克鲁伊森

Answers:


15

R,20个字节

这是你所追求的吗?用于table计算每个scan输入值的出现次数。测试count是否大于1并求和。

sum(table(scan())>1)

在线尝试!


duplicated首先想到的table是-谦虚对打高尔夫球非常有用!
朱塞佩

@giuseppe表是现在的最爱:)
MickyT




6

C(铛) 175个 117 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);}

在线尝试!

这是我第一次提交其中之一,因此请告知格式是否存在任何问题。

来自评论的更新:

  • Jo King的-58至117字节
  • -80至95个字节(仅ASCII)

原始提交


5
欢迎您,美好的开始。我不是C语言人,但这里是指向高尔夫球C页面技巧的链接
MickyT

2
117个字节=> 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;}。正如仅@ASCII指出的那样,includes不会影响程序的编译
Jo King

2
@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)

1
@CollinPhillips是的。正如您在我发布的链接中看到的那样,它在没有包含内容的情况下仍可以正常编译
(仅ASCII)

2
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码

5

C#(Visual C#交互式编译器),40字节

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

规范的初稿尚不清楚,我认为这意味着返回所有出现的元素不止一次。这是更新的版本。

不知何故,我没有注意到我的代码返回了出现一次的元素数量。感谢Paul Karam抓住了这一点!

在线尝试!


1
您的输出是错误的,它需要计算2次或更多次出现的元素。应该是n=>n.GroupBy(c=>c).Count(c=>c.Count()>=2)。该任择议定书说,这个名单的答案是2.您的代码返回5.我给你的变化返回2
保罗·卡拉姆

1
或者只是>1为了保持40个字节的计数
Paul Karam

@PaulKaram我没注意到,谢谢!
无知的体现


4

J11 9字节

-2字节感谢乔纳!

1#.1<1#.=

在线尝试!

原始解决方案:

1#.(1<#)/.~

在线尝试!

说明:

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

嘿,加伦。 1#.1<1#.=9个字节+很好的自分类乐趣。
乔纳

1
@Jonah谢谢!老实说,我没有意识到这一点。
Galen Ivanov

1
@乔纳尼斯
亚当

@Adám,在这里我很高兴我得到J与APL的联系。再次被骗:)
约拿



3

果冻,4 字节

ĠITL

在线尝试!

...要么 ĠIƇL

怎么样?

Ġ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[[6],[3,6]])的真实结果,该结果也具有所需的长度。




3

Java 8,74 73字节

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

在线尝试。

说明:

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



3

Haskell,41个字节

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

此解决方案基本上是计算列表中有多少个元素具有相同的元素,而该元素恰好在列表中稍后出现一次。


2

Haskell,47个字节

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

在线尝试!

这是幼稚的方法。可以采取一些措施来改善这一点。

f[]=0

我们返回0空列表

f(a:b)

如果是非空列表,则以开头,a然后是b

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

如果过滤a出的b是从不同的b(即a是在b),那么我们返回1大于f施加到baš过滤掉。

|1>0=f b

如果过滤器as不变,b那么我们f将遍历其余部分。

这是另一种具有相同长度的类似方法:

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

在线尝试!



2

Wolfram语言34个字节

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

Gather将相同的整数分组到列表中。 DeleteCases[...{x_}]消除包含单个数字的列表。 Length返回剩余列表的数量(每个列表包含两个或多个相同的整数。


1
Count[{_,__}]@*Gather
alephalpha


2

Pyth,6个字节

l{.-Q{

在这里尝试

说明

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


2

PHP,39字节

一个使用变量变量的好机会:

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

从命令行参数获取输入。运行-nr在线尝试


$argv[0]-,并且仅在参数中出现一次,因此不会影响结果。


1

元素,40字节

_(#'{"2:0+4:'~1+";~2=[''1+""]$2+'[(#]'}`

在线尝试!

这就要求输入必须采用精确的格式,例如[234, 2, 1000, 2, 99, 234][]用逗号和整数之间的空格括起来)。

说明:

_                                        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

1

视网膜0.8.2,19字节

O`.+
m`^(.+)(¶\1)+$

在线尝试!链接包括测试套件,该套件将逗号分隔成每一行。说明:

O`.+

将相等的值排序在一起。

m`^(.+)(¶\1)+$

计算至少两个值的运行次数。


1

干净59 54字节

import StdEnv,StdLib
$l=sum[1\\[_,_:_]<-group(sort l)]

在线尝试!

对列表进行排序,将相邻的相等元素分组,并用1个以上的项目进行计数。


1

锈,126字节

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()};

我放弃。这基本上与Ruby相同。还有一种“另一种方式”可以使用输入向量+100000中的值创建一个数组并为其建立索引,但是类型转换(如usize / as i32)占用太多空间。



1

k,8个字节

+/1<#:'=

读取为:总和(每组的长度)> 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)

使用示例(第一个测试用例)

+/1<#:'=1 10 16 4 8 10 9 19 2 15 18 19 10 9 17 15 19 5 13 20

写4

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.