如何找到数组中第二大元素的索引?
How can I find the index of the second greatest element in my array?
我正在使用 Ruby 2.4,我有一个数字数组:
[23423, 349843, 13123, 29239, 20201, ...]
如何找到数组中第二大值对应的数组索引?您可以假设数组中至少有两个元素。
试试这个。 a
是你的数组
a.index(a.max(2).last)
如果你需要这个值,试试这个
value = array.max(2).last
如果你需要索引,试试这个
index = array.each_with_index.max_by(2, &:first).last.last
这是如何工作的?
each_with_index
创建一个包含 [element, index]
元组的枚举器
max_by(2, &:first)
找到两个最大的元组,比较它们的第一个值也就是元素
last
获取 second-greatest 元组
last
通过获取最后一个值也就是索引 来解包该元组
请注意,这会创建 O(n)
临时数组,因为我们链接了 each_with_index
枚举器,我不会将其用于性能关键代码路径中的大型数组。
我会对数组进行排序,然后使用类似的东西:
ary.size - 2
例如:
ary = 5.times.map{ rand(100) } # => [61, 75, 35, 48, 59]
ary.sort # => [35, 48, 59, 61, 75]
ary.sort[-2] # => 61
ary.size - 2 # => 3
ary.sort[ary.size - 2] # => 61
This does not return the index of the element in the original array.
排序后第二大元素的索引总是array.size - 2
.
如果数组必须按其原始顺序排列,我会这样做:
ary = 5.times.map{ rand(100) } # => [83, 72, 4, 63, 68]
hash = ary.each_with_index.to_h # => {83=>0, 72=>1, 4=>2, 63=>3, 68=>4}
hash.sort[-2] # => [72, 1]
此时 hash.sort[-2]
returns 值及其在原始数组中的索引。 72
是值,ary[1]
是值的索引。
a = [1,3,1,2]
当1
和1
作为a
的两个最小值时
def second_largest_not_uniq(a)
a.each_index.min_by(2) { |i| a[i] }[1]
end
second_largest_not_uniq [1,3,1,2] #=> 2
second_largest_not_uniq [1] #=> nil
second_largest_not_uniq [] #=> nil
当1
和2
作为a
的两个最小值
def second_largest_uniq(a)
a.each_index.to_a.uniq { |i| a[i] }.min_by(2) { |i| a[i] }[1]
end
second_largest_uniq [1,3,1,2] #=> 3
second_largest_uniq [1,1,1] #=> nil
second_largest_uniq [] #=> nil
second_largest_uniq [1] #=> nil
我正在使用 Ruby 2.4,我有一个数字数组:
[23423, 349843, 13123, 29239, 20201, ...]
如何找到数组中第二大值对应的数组索引?您可以假设数组中至少有两个元素。
试试这个。 a
是你的数组
a.index(a.max(2).last)
如果你需要这个值,试试这个
value = array.max(2).last
如果你需要索引,试试这个
index = array.each_with_index.max_by(2, &:first).last.last
这是如何工作的?
each_with_index
创建一个包含[element, index]
元组的枚举器
max_by(2, &:first)
找到两个最大的元组,比较它们的第一个值也就是元素last
获取 second-greatest 元组last
通过获取最后一个值也就是索引 来解包该元组
请注意,这会创建 O(n)
临时数组,因为我们链接了 each_with_index
枚举器,我不会将其用于性能关键代码路径中的大型数组。
我会对数组进行排序,然后使用类似的东西:
ary.size - 2
例如:
ary = 5.times.map{ rand(100) } # => [61, 75, 35, 48, 59]
ary.sort # => [35, 48, 59, 61, 75]
ary.sort[-2] # => 61
ary.size - 2 # => 3
ary.sort[ary.size - 2] # => 61
This does not return the index of the element in the original array.
排序后第二大元素的索引总是array.size - 2
.
如果数组必须按其原始顺序排列,我会这样做:
ary = 5.times.map{ rand(100) } # => [83, 72, 4, 63, 68]
hash = ary.each_with_index.to_h # => {83=>0, 72=>1, 4=>2, 63=>3, 68=>4}
hash.sort[-2] # => [72, 1]
此时 hash.sort[-2]
returns 值及其在原始数组中的索引。 72
是值,ary[1]
是值的索引。
a = [1,3,1,2]
当1
和1
作为a
def second_largest_not_uniq(a)
a.each_index.min_by(2) { |i| a[i] }[1]
end
second_largest_not_uniq [1,3,1,2] #=> 2
second_largest_not_uniq [1] #=> nil
second_largest_not_uniq [] #=> nil
当1
和2
作为a
def second_largest_uniq(a)
a.each_index.to_a.uniq { |i| a[i] }.min_by(2) { |i| a[i] }[1]
end
second_largest_uniq [1,3,1,2] #=> 3
second_largest_uniq [1,1,1] #=> nil
second_largest_uniq [] #=> nil
second_largest_uniq [1] #=> nil