Skip to content

Instantly share code, notes, and snippets.

@aarkerio
Created January 20, 2020 19:41
Show Gist options
  • Save aarkerio/d8527016aeeed64aabb03764476ea7c5 to your computer and use it in GitHub Desktop.
Save aarkerio/d8527016aeeed64aabb03764476ea7c5 to your computer and use it in GitHub Desktop.
Remove duplicated anagrams from Array
class Anagram
def initialize
@unique_anagrams = nil
end
def my_array
@unique_anagrams ||= ["perro", "algo", "qwer","uuu", "lujo", "peorr","lago","edfr", "sadasd", "uljo", "erpro", "perro", "jolu"]
end
def get_clean_array
ma = my_array
p ma
unique_strings = homologate_array ma
indexed_results = indexing unique_strings
indexes = grouping indexed_results
p ">> After grouping >>>> #{indexes}"
findexes = flatting indexes
p ">> After flatting >>>> #{findexes}"
ss = pick_indexes findexes
p ">> After picking indexes >>>> #{ss.class} >>>> #{ss}"
ma.delete_if.with_index { |_, index| ss.include? index }
p ">> final ma >>>> #{ma}"
end
private
def homologate_array(words)
words.map {|w| w.downcase.split('').sort!.join('')}
end
def grouping(arr)
arr.group_by { |name| name[0] }
end
def flatting(arr)
arr.map { |k, v| v.drop(1).flatten }
end
def pick_indexes(arr)
arr.map do |i|
i.reject { |e| e.respond_to?(:to_str) }
end.flatten
end
def indexing(arr)
arr.each_with_index.select { |i, idx| arr.count(i) > 1 }
end
end
Anagram.new.get_clean_array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment