开发者

Numbers = 1-5, List = [1,3,4]. How to get back a list [2,5]?

开发者 https://www.devze.com 2023-02-23 15:18 出处:网络
开发者_如何学运维I think that the question title explains itself :) Suppose that i have the the numbers 1,2,3,4 and 5.

开发者_如何学运维I think that the question title explains itself :) Suppose that i have the the numbers 1,2,3,4 and 5. Now, i have a list [1,3,4].

What is the best way to get back the inverted list ? A list like [2,5] with the missing elements ?


As simple as

> [1,2,3,4,5] - [1,3,4]
=> [2, 5] 

Documentation here.


Use the splat operator to create an array from your range of numbers and then subtract out the set

> [*1..5]-[1,3,4]
 => [2, 5] 
0

精彩评论

暂无评论...
验证码 换一张
取 消