Possible Duplicate:
diff a ruby string or array
I have an old array: [1, 2, 3, 4, 5]
, and new: [1, 2, 4, 6]
How to get difference with Ruby: that 5, 3
was removed and 6
was added?
select { array. count } is a nested loop, you're doing an O(n^2) complex algorithm for something which can be done in O(n). You're right, to solve this Skizit's question we can use in O(n); but in order to find out which elements are duplicated an O(n^2) algo is the only way I can think of so far.
Arrays can be equal if they have the same number of elements and if each element is equal to the corresponding element in the array. To compare arrays in order to find if they are equal or not, we have to use the == operator.
first is a property of an array in Ruby that returns the first element of an array. If the array is empty, it returns nil. array. first accesses the first element of the array, i.e., the element at index 0 .
Ruby's arrays and hashes are indexed collections. Both store collections of objects, accessible using a key. With arrays, the key is an integer, whereas hashes support any object as a key. Both arrays and hashes grow as needed to hold new elements.
irb(main):001:0> a = [1, 2, 3, 4, 5] => [1, 2, 3, 4, 5] irb(main):002:0> b = [1, 2, 4, 6] => [1, 2, 4, 6] irb(main):003:0> a - b => [3, 5] irb(main):005:0> b - a => [6] irb(main):006:0>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With