且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查数组中的所有元素是否出现不超过两次?

更新时间:2023-11-28 20:45:16

在我看来,这可能是一个非常简单的解决方案:

In my point of view, that could be a pretty simple solution:

def no_more_than_twice_occur?(array)
  array.none? { |el| array.count(el) > 2 }
end


no_more_than_twice_occur?(["1", "3", "3", "55", "3", "2"]) # => false
no_more_than_twice_occur?(["20", "10", "20", "10"]) # => true