免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4422 | 回复: 1
打印 上一主题 下一主题

ruby array中找出重复元素 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-14 19:26 |只看该作者 |倒序浏览
ruby array中找出重复元素












Ruby代码
  1. 1.#上文的inject现在用上了  
  2. 2.module Enumerable  
  3. 3.  def dups  
  4. 4.    inject({}) {|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys  
  5. 5.  end  
  6. 6.end  
复制代码
Ruby代码
  1. 1.arr = %w{foo bar baz bar baz qux foo zub}  
  2. 2.puts arr.dups.inspect  
  3. 3.# => ["baz", "foo", "bar"]  
复制代码
Ruby代码
  1. 1.#替换一  
  2. 2.inject(Hash.new(0)) {|h,v| h[v] += 1; h}.reject{|k,v| v==1}.keys  
复制代码
这个是比较好理解的



Ruby代码
  1. 1.class Array  
  2. 2.  def only_duplicates  
  3. 3.    duplicates = []  
  4. 4.    self.each {|each| duplicates << each if self.count(each) > 1}  
  5. 5.    duplicates.uniq  
  6. 6.  end  
  7. 7.end  
复制代码
Ruby代码
  1. 1.puts [1,2,2,4,5,1].only_duplicates.inspect  
  2. 2.==> [1, 2]  
复制代码
Ruby代码
  1. 1.require 'benchmark'   
  2. 2.  
  3. 3.module Enumerable  
  4. 4.   def map_with_index  
  5. 5.      index = -1  
  6. 6.      (block_given? && self.class == Range || self.class == Array)  ?  map { |x| index += 1; yield(x, index) }  :  self  
  7. 7.   end  
  8. 8.end  
  9. 9.  
  10. 10.  
  11. 11.class Array  
  12. 12.  
  13. 13.   def find_dups  
  14. 14.      inject(Hash.new(0)) { |h,e| h[e] += 1; h }.select { |k,v| v > 1 }.collect { |x| x.first }  
  15. 15.   end  
  16. 16.  
  17. 17.  
  18. 18.  
  19. 19.  
  20. 20.   def find_dups2  
  21. 21.      uniq.select{ |e| (self-[e]).size < self.size - 1 }  
  22. 22.   end  
  23. 23.  
  24. 24.   def find_ndups     # also returns the number of items  
  25. 25.      uniq.map { |v| diff = (self.size - (self-[v]).size); (diff > 1) ? [v, diff] : nil}.compact  
  26. 26.   end  
  27. 27.  
  28. 28.  
  29. 29.   # cf. http://www.ruby-forum.com/topic/122008  
  30. 30.   def dups_indices     
  31. 31.      (0...self.size).to_a - self.uniq.map{ |x| index(x) }  
  32. 32.   end  
  33. 33.  
  34. 34.   def dup_indices(obj)  
  35. 35.      i = -1  
  36. 36.      ret = map { |x| i += 1; x == obj ? i : nil }.compact  
  37. 37.      #ret = map_with_index { |x,i| x == obj ? i : nil }.compact  
  38. 38.      ret.shift  
  39. 39.      ret  
  40. 40.   end  
  41. 41.  
  42. 42.   def delete_dups(obj)  
  43. 43.      indices = dup_indices(obj)  
  44. 44.      return self if indices.empty?  
  45. 45.      indices.reverse.each { |i| self.delete_at(i) }  
  46. 46.      self  
  47. 47.   end  
  48. 48.  
  49. 49.end   
  50. 50.  
  51. 51.  
  52. 52.array = [1,3,5,5,6,7,9,10,14,18,22,22,4,4,4,3,6]  
  53. 53.  
  54. 54.dups = nil  
  55. 55.  
  56. 56.  
  57. 57.Benchmark.bm(14) do |t|   
  58. 58.  
  59. 59. t.report('find_dups:') do  
  60. 60.    dups = array.find_dups  
  61. 61. end   
  62. 62.  
  63. 63.end   
  64. 64.  
  65. 65.p dups   #=> [5, 22, 6, 3, 4]  
  66. 66.  
  67. 67.  
  68. 68.p %w(a b a c c d).dups_indices  
  69. 69.p %w(a b a c c d).dup_indices('c')  
  70. 70.p %w(a b a c c d).delete_dups('a')  
复制代码
Ruby代码
  1. 1.class Array  
  2. 2.  
  3. 3.  def find_dups  
  4. 4.    uniq.map {|v| (self - [v]).size < (self.size - 1) ? v : nil}.compact  
  5. 5.  end  
  6. 6.  
  7. 7.end  
复制代码
Ruby代码
  1. 1.def mostused(ahash)  
  2. 2.   original_size = ahash.size  
  3. 3.   sizes = {}     
  4. 4.   ahash.uniq.map { |x| sizes[x] = original_size - ((ahash - [x]).size)}  
  5. 5.   sizes.sort_by {|k,v| v}.pop        
  6. 6. end
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-19 18:09 |只看该作者
牛呀!!太有才了.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP