Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Ruby数组中删除顺序重复项_Ruby - Fatal编程技术网

从Ruby数组中删除顺序重复项

从Ruby数组中删除顺序重复项,ruby,Ruby,假设我有以下数组,我想去掉连续的重复项: arr = [1,1,1,4,4,4,3,3,3,3,5,5,5,1,1,1] 我想得到以下信息: => [1,4,3,5,1] 如果有比我的解决方案(或其变体)更简单、更高效的解决方案,那就太好了: 或 如果我们使用: arr = 10000.times.collect { rand(4) + 1 } # less likelihood of repetition 我们得到: agis is faster than arupv3 by 19

假设我有以下数组,我想去掉连续的重复项:

arr = [1,1,1,4,4,4,3,3,3,3,5,5,5,1,1,1]
我想得到以下信息:

=> [1,4,3,5,1]
如果有比我的解决方案(或其变体)更简单、更高效的解决方案,那就太好了:

如果我们使用:

arr = 10000.times.collect { rand(4) + 1 } # less likelihood of repetition
我们得到:

agis is faster than arupv3 by 19.999999999999996% ± 10.0%
arupv3 is faster than abdo2 by 1.9x ± 0.1
abdo2 is similar to abdo
abdo is faster than arupv2 by 2.1x ± 0.1
arupv2 is similar to arup
使用以下方法进行操作:

更新

根据评论,这里是另一个选择:

arr.join.squeeze.chars.map(&:to_i)
# => [1, 4, 3, 5, 1]
另一种选择

arr.each_with_object([]) { |el, a| a << el if a.last != el }
arr.each_with_object([]){| el,a | a不太优雅但最有效的解决方案:

require 'benchmark'

arr = [1,1,1,4,4,4,3,3,3,3,5,5,5,1,1,1]

GC.disable
Benchmark.bm do |x|
  x.report do
    1_000_000.times do
      i = 1
      a = [arr.first]

      while i < arr.size
        a << arr[i] if arr[i] != arr[i-1]
        i += 1
      end
    end
  end
end
#      user     system      total        real
# 1.890000   0.010000   1.900000 (  1.901702)

GC.enable; GC.start; GC.disable

Benchmark.bm do |x|
  x.report do
    1_000_000.times do
      (arr + [nil]).each_cons(2).collect { |i| i[0] != i[1] ? i[0] : nil }.compact
    end
  end
end
#      user     system      total        real
# 6.050000   0.680000   6.730000 (  6.738690)
需要“基准测试”
arr=[1,1,1,4,4,3,3,3,5,5,1,1,1]
禁用
Benchmark.bm do|x|
x、 举报
一千倍
i=1
a=[arr.first]
而我a看起来我的更快:s..我会发布基准测试above@Abdo完成..不删除(从原始注释框中)你的链接评论在我的回答中。:@Abdo再次检查!:)join.squage
方法只适用于一位数。第一个解决方案非常好:它简洁易懂,适用于任何可比较的数据类型,并且几乎可以肯定足够快。@WayneConrad我喜欢不同的(教育性的)
join.squeak
采取的方法,以防我们使用字符与数字,我很高兴aruprakshit提供了替代方案:-)“我将在响应出现时对其进行基准测试”…然后接受第一个答案,确保不会出现响应…我相信人们(尤其是Rubyists)回答被接受后不要停止发帖。@MarkThomas我完全理解你的观点,但同时,在过去的几天里,我见过很多人(比如:aruprakshit、careswoveland、steenslag、matt)回答前一段时间的问题并改进它们只是因为他们喜欢!为什么你更喜欢一个稍微快一点的解决方案而不是一个更清晰的解决方案?这段代码是你应用程序中的一个瓶颈吗?@WayneConrad我已经接受了下面更清晰的解决方案,你可以看到=)检查我对Agis回答的评论:-)是的,这至少是t比我的最快解决方案快2倍。(我的基准测试不同,因为我使用的是更大的数组)。但是,您的解决方案有问题:您可以尝试:
agis([0,1,4,4,4,3,3,3,3,5,5,5,1])
=>返回
[0,0,1,4,3,5,1]
预期:
[0,1,4,5,1]
。修复后请告诉我,这样我就可以在我的基准测试中发布=)我怀疑,有多少Ruby编码器会使用这种循环来解决此类问题:)@Agis,你得到的是+1。ArupRakshit的上一个解决方案在效率上与你的解决方案非常接近。然而,他的解决方案在视觉上要容易得多。我的问题要求既简单又有效是的。
agis is faster than arupv3 by 19.999999999999996% ± 10.0%
arupv3 is faster than abdo2 by 1.9x ± 0.1
abdo2 is similar to abdo
abdo is faster than arupv2 by 2.1x ± 0.1
arupv2 is similar to arup
arr = [1,1,1,4,4,4,3,3,3,3,5,5,5,1,1,1]
arr.chunk { |e| e }.map(&:first)
# => [1, 4, 3, 5, 1]
# if you have only **Fixnum**, something magic
arr.chunk(&:to_i).map(&:first)
# => [1, 4, 3, 5, 1]
arr.join.squeeze.chars.map(&:to_i)
# => [1, 4, 3, 5, 1]
arr.each_with_object([]) { |el, a| a << el if a.last != el }
require 'benchmark'

arr = [1,1,1,4,4,4,3,3,3,3,5,5,5,1,1,1]

GC.disable
Benchmark.bm do |x|
  x.report do
    1_000_000.times do
      i = 1
      a = [arr.first]

      while i < arr.size
        a << arr[i] if arr[i] != arr[i-1]
        i += 1
      end
    end
  end
end
#      user     system      total        real
# 1.890000   0.010000   1.900000 (  1.901702)

GC.enable; GC.start; GC.disable

Benchmark.bm do |x|
  x.report do
    1_000_000.times do
      (arr + [nil]).each_cons(2).collect { |i| i[0] != i[1] ? i[0] : nil }.compact
    end
  end
end
#      user     system      total        real
# 6.050000   0.680000   6.730000 (  6.738690)