我知道在 Groovy 中, 如果
list = [1,2,3,1]
什么时候
list.unique()
带返回
[1,2,3]
但是如果我想检测列表中重复的、不连续的项目的重复值。我怎样才能做到这一点?
detect([1,2,3,1]) => true
detect([1,2,3,2]) => true
detect([1,1,2,3]) => false
detect([1,2,2,3,3]) => false
detect([1,2,3,4]) => false
谢谢。
编辑: 添加这两种情况
detect([1,2,2,1]) => true
detect([1,2,1,1]) => true
true 表示发生任何非连续、重复的情况。
最佳答案
这应该可以做到:
List list = ["a", "b", "c", "a", "d", "c", "a"]
和
list.countBy{it}.grep{it.value > 1}.collect{it.key}
关于Groovy:检测列表中重复、不连续值的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16827427/