0

我有一个包含

arrTV = ['Thor: The Dark World', 'Ender's Game', 'Jackass Presents: Bad Grandpa', 'Last Vegas', 'Free Birds', 'Free Birds' ]

最后两个元素是重复的。我想遍历数组并检查是否存在重复并且我不想使用.uniq

请指教

我已经尝试过这样的事情,但它不起作用

arrTV.each do |i|
  count = 0

  #if arrTV[i] == arrTV[i+1]
  #  puts "equal"
  #  count = count + 1
  #
  #end
  #puts count

  #if arrTV[i] = arrTV[i+1]
  #arrTV.delete_at(i+1)
  #end

end

我哪里错了?

4

6 回答 6

3

这可能不是最有效的,但很容易从高层次上理解正在发生的事情。

require 'set'

arrTV = ['Thor: The Dark World', 'Ender's Game', 'Jackass Presents: Bad Grandpa', 'Last Vegas', 'Free Birds', 'Free Birds' ]

arrTV.to_set.to_a
于 2013-11-04T23:20:33.963 回答
1

你可以这样做:

arr = []
arrTV.each do |e|
     arr << e unless arr.include?(e)
end
#arr should now contains the same elements as arrTV.uniq does.

但是,为什么不使用uniq呢?家庭作业可能吗?

于 2013-11-04T23:25:55.287 回答
1

怎么样:

2.0.0-p247 :004 > arrTV = ['Thor: The Dark World', 'Ender\'s Game', 
'Jackass Presents: Bad Grandpa', 'Last Vegas', 'Free Birds', 'Free Birds' ]  
# (Note escaping the `'` in Ender`'`s)

2.0.0-p247 :009 > prev='xxxxx'
2.0.0-p247 :009 > new_array=[]

2.0.0-p247 :016 > arrTV.sort.each do |current|
2.0.0-p247 :017 >     if (current != prev)
2.0.0-p247 :018?>       new_array << current
2.0.0-p247 :018?>       puts current
2.0.0-p247 :019?>       prev=current
2.0.0-p247 :020?>     end
2.0.0-p247 :021?>   end
Ender's Game
Free Birds
Jackass Presents: Bad Grandpa
Last Vegas
Thor: The Dark World
于 2013-11-04T23:15:30.147 回答
1

我认为你可以使用Array#|方法,如果你不想使用Array#uniq方法。

arr = %w(foo bar baz bar)
(arr | arr)
# => ["foo", "bar", "baz"]
于 2013-11-05T03:34:31.427 回答
0

我将把我的评论限制在你的最后一句话:“我哪里错了?”

arrTV = ['Thor: The Dark World', 'Ender's Game', 'Jackass Presents: Bad Grandpa', \
         'Last Vegas', 'Free Birds', 'Free Birds' ]

[编辑:正如@Michael 指出的那样,您必须arrTV转义(特别是)元素中的所有撇号'Ender\'s Game'。或者,您可以将字符串括在双引号中:

arrTV = ["Thor: The Dark World", "Ender's Game", "Jackass Presents: Bad Grandpa", \
         "Last Vegas", "Free Birds", "Free Birds" ]

我建议做后者。编辑结束]

看来您首先尝试了这个:

arrTV.each do |i|
  count = 0
  if arrTV[i] == arrTV[i+1]
    puts "equal"
    count = count + 1
  end
  puts count
end

第一次通过 ( do..end) 块,i设置为等于 的第一个元素arrTV,即'Thor: The Dark World',因此您正在尝试执行

  if arrTV['Thor: The Dark World'] == arrTV['Thor: The Dark World'+1]

这将引发错误。考虑改为这样做

  if arrTV[count] == arrTV[count+1]

更好,但仍然存在一些问题。首先,每次将块传递给 的元素时arrTVcount都将其重置为零。你可以通过移动count = 0before来解决这个问题arrTV.delete_at(i+1)。此外,移动puts countif语句的末尾(就在 之前end)。

其次,什么时候发生count = 5?那时你正在执行

  if arrTV[5] == arrTV[6],

arrTV只包含 6 个元素,最后一个是 indexed 5arrTV[6]没有定义。有很多方法可以解决这个问题。一是添加:

  break if count == 5

  count = count + 1

看起来您接下来尝试在块内执行此操作

   if arrTV[i] = arrTV[i+1]
   arrTV.delete_at(i+1)

(顺便说一句,请注意您需要==,而不是=在第一条语句中。)即使我们i用 count 替换,您也试图在arrTV对其进行交互时删除一些元素。那是不行的。你需要做的是从这个开始:

  (0..arrTV.size-2) each do |i|

有了这个,你不再需要count. 处理完arrTV. 更重要的是,由于您没有遍历 的元素arrTV,因此可以在循环中删除它们:

   arrTV.delete_at(i+1)

(这样,您还可以向arrTV中添加元素或修改元素。)我将留给您从这里修复它。但是,我想指出的是,您采用的方法只会消除重复元素,如果它们是arrTV. 如果顺序不重要,则需要修改方法。

最后,一个 Ruby 约定是仅使用小写字母作为变量名,并在必要时添加下划线以提高可读性。您可能会使用类似的东西arr_tv,而不是arrTV.

于 2013-11-05T00:13:02.240 回答
0

您可以将 Set Intersection 方法与 & 号 ( &) 运算符一起使用:

arrTV = ['Thor: The Dark World', 'Ender\'s Game', 
'Jackass Presents: Bad Grandpa', 'Last Vegas', 'Free Birds', 'Free Birds' ] 

def unique(array)
  array & array
end

result = unique(arrTV)
print result # => ["Thor: The Dark World", "Ender's Game", "Jackass Presents: Bad Grandpa", "Last Vegas", "Free Birds"]

使用Set Intersection(使用 Intersect Operator &),返回一个新数组,其中包含两个数组共有的元素,并删除了重复项。

在该方法中,&放置在我们要评估的两个数组之间。在此示例中,我们将一个数组传递给该方法,但对该数组进行自我评估以删除任何重复项。

于 2016-02-10T08:07:14.303 回答