检查两个阵列具有相同的内容(以任何顺序) [英] Check if two arrays have the same contents (in any order)

查看:84
本文介绍了检查两个阵列具有相同的内容(以任何顺序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ruby 1.8.6和Rails 1.2.3,并且需要确定两个数组是否有相同的元素,不管他们是否是不以相同的顺序。一阵列的保​​证不会包含重复(另一威力,在这种情况下,答案是否定的)。

I'm using Ruby 1.8.6 with Rails 1.2.3, and need to determine whether two arrays have the same elements, regardless of whether or not they're in the same order. One of the arrays is guaranteed not to contain duplicates (the other might, in which case the answer is no).

我的第一个想法是

require 'set'
a.to_set == b.to_set

但我想知道是否有这样做的更有效的或习惯的方法。

but I was wondering if there was a more efficient or idiomatic way of doing it.

推荐答案

这并不需要转换设置:

a.uniq.sort == b.uniq.sort

这篇关于检查两个阵列具有相同的内容(以任何顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆