开发者

Comparing array of structs, and removing duplicate

开发者 https://www.devze.com 2022-12-30 20:59 出处:网络
I have two arrays of structs. array_of_structs1 array_of_structs2 The开发者_JAVA百科 struct class looks like this, for contextual info:

I have two arrays of structs.

array_of_structs1
array_of_structs2

The开发者_JAVA百科 struct class looks like this, for contextual info:

class Leader < Struct.new(:rank, :user); end

I want to remove the duplicate users from array_of_structs1.

Any assistance would be greatly appreciated!


I'm not sure if I understand. If you want to remove duplicate structs that have the same user in array_of_struct1 use:

array_of_structs1 = Hash[*array_of_structs1.map {|obj| [obj.user, obj]}.flatten].values

If you wan't to remove entries from array1 that are also in array 2 use

array_of_structs1 = array_of_structs1 - array_of_struct2
0

精彩评论

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