How to find the same item in an array with Ruby -
i have array this.
@arr = ["ac", "ba", "ca", "dd", "ac", "bd", "ca", "dd"]
i want swap letter if first 1 capital. in case, ac becomes ca, ba -> ab etc. becomes this.
@arr = ["ca", "ab", "ac", "dd", "ac", "bd", "ca", "dd"]
then want find same item. in case there two, ca , dd.
@newarr = ["ca", "dd"]
thanks in advance.
====
this got far.
@firstarr = @arr.map{|item| if item[0] =~ /[a-z]/ item = item[1]+item[0] else itme = item end }
this gives
@firstarr = ["ca", "ab", "ac", "dd", "ac", "bd", "ca", "dd"]
for reversing criteria:
foo = @arr.map |x| (x[0].downcase!).nil? ? x : x.reverse end # => ["ca", "ab", "ac", "dd", "ac", "bd", "ca", "dd"]
downcase!
returns nil
if receiver in downcase, using this, above code checks if x[0]
can downcase-d; if yes, means first character in uppercase , reverse word, else returns same word.
for duplication criteria:
foo.select { |x| foo.count(x) > 1 }.uniq # => ["ca", "ac", "dd"]
Comments
Post a Comment