如何有效地从正整数和负整数数组中删除正整数的负重复,如下所示: [1,5,10,5--5,-1,9] 结果,我希望: [1,5,10,5,9] (-1和-5被删除,因为它们是1和5的负重复)
答案 0 :(得分:6)
这是我能找到的最简单的方法:
array = [1, 5, 10, 5, -5, -1, 9]
p array - array.select{ |i| i > 0 }.map{ |i| -i }
# [1, 5, 10, 5, 9]
它使用Array#-
,这应该相当快。
答案 1 :(得分:5)
您可以在O(n)
中执行此操作,只需两次通过数组,然后对正数进行散列,然后从绝对值为哈希的数组负值中拒绝:
def reject_neg_dups(arr)
positives = Hash[arr.map {|x| (x>0) ? [x,1] : nil }.compact]
arr.reject { |x| (x < 0) && positives[-x] }
end
reject_neg_dups([-1, 1, 2, -2]) # => [1, 2]
reject_neg_dups([-1, 1, -2]) # => [1, -2] since 2 does not appear
有趣的是,the Array-
solutions比目前列出的其他人快得多:
require 'benchmark'
def reject_neg_dups_hash(arr)
positives = Hash[arr.map {|x| (x>0) ? [x,1] : nil }.compact]
arr.reject { |x| (x < 0) && positives[-x] }
end
def reject_neg_dups_include(arr)
arr.reject { |x| (x < 0) && arr.include?(x.abs) }
end
def reject_neg_dups_arrayminus(arr)
arr - arr.select { |i| i > 0 }.map { |i| -i }
end
def reject_neg_dups_arrayminusewo(arr)
arr - arr.each_with_object([]) { |n,b| b << -n if n > 0 }
end
arr = Array.new(1000) { rand(-100..100) }
N = 1000
Benchmark.bm(15) do |x|
x.report('Array-') { N.times { reject_neg_dups_arrayminus(arr.dup) } }
x.report('Array-ewo') { N.times { reject_neg_dups_arrayminusewo(arr.dup) } }
x.report('hash') { N.times { reject_neg_dups_hash(arr.dup) } }
x.report('include?') { N.times { reject_neg_dups_include(arr.dup) } }
end
示例输出:
user system total real
Array- 0.180000 0.000000 0.180000 ( 0.187512)
Array-ewo 0.200000 0.000000 0.200000 ( 0.194663)
hash 0.250000 0.010000 0.260000 ( 0.253355)
include? 3.660000 0.000000 3.660000 ( 3.666313)
答案 2 :(得分:4)
您可以使用Array#reject:
轻松完成此操作>> a = [1, 5, 10, 5, -5, -1, 9]
>> a.reject { |e| e < 0 && a.include?(e.abs) }
=> [1, 5, 10, 5, 9]
为了澄清另一个例子,这不会删除数组中没有相应正值的负值:
>> b = [1, 5, 10, 5, -5, -1, 9, -15]
>> b.reject { |e| e < 0 && b.include?(e.abs) }
=> [1, 5, 10, 5, 9, -15]
您可以定义如下方法:
def reject_negative_duplicates(array)
array.reject { |e| e < 0 && array.include?(e.abs) }
end
>> reject_negative_duplicates(a)
=> [1, 5, 10, 5, 9]
>> reject_negative_duplicates(b)
=> [1, 5, 10, 5, 9, -15]
或扩展(猴子补丁)数组:
class Array
def reject_negative_duplicates
self.reject { |e| e < 0 && self.include?(e.abs) }
end
end
>> a.reject_negative_duplicates
=> [1, 5, 10, 5, 9]
>> b.reject_negative_duplicates
=> [1, 5, 10, 5, 9, -15]
答案 3 :(得分:3)
arr = [1, 5, 10, 0, 5, -5, -1, 9, -4]
arr - arr.each_with_object([]) { |n,b| b << -n if n > 0 }
#=> [1, 5, 10, 0, 5, 9, -4]
答案 4 :(得分:0)
在Ruby中,您可以使用uniq方法:https://ruby-doc.org/core-2.2.0/Array.html#method-i-uniq。
否则,您需要检查this one之类的答案,其中包含如何构建迭代数组的结构。