简体   繁体   中英

How do i find if element is not in a ruby array?

Ok so i have this array

 array
 => [1620, 3093] 

and I have a integer

 num
 => 1620 

is there an easy way to see if there is another number in the array that is not num

so for example

is there another number in the array that doesnt match num . So for the above example i would return true but if array was [1620, 1620] then i would return false

arr.any?{|x| x != num }

上面的应该工作正常,也可读高效!

array.select{|array_num| array_num != num}.length > 0

编辑:甚至更清洁:

(array - [num]).empty?
array.reject{ |a| a == num }.size > 0

Join the sorted array with a separator and find if there is a match with 2 adjacent numbers.

array.sort.join(",").include?("#{num},#{num}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM