From: yermej Date: 2007-11-27T02:15:01+09:00 Subject: Re: Removing duplicates and substrings from an array On Nov 26, 9:15 am, Sam Larbi wrote: > Note: parts of this message were removed by the gateway to make it a legal Usenet post. > > I've got an array of strings, say like: > > ["Bob", "John", "Bobby", "John"] > > I want to remove duplicates and elements that are substrings of other > elements. Therefore, the above array would become: > > ["John","Bobby"] > > (order doesn't really matter to me, BTW) > > Right now, this is what I'm doing: > > def remove_duplicates_and_subsequences(some_array) > result = [] > some_array.each_index do |i| > (some_array.length-1).downto 0 do |j| > some_array.delete_at(j) if i != j && > some_array[i].index(some_array[j]) > end > end > return result > end > > Is there a better way to do that? I feel like I should be using select or > reject, but can't think of a way to do it. > > Thanks, > Sammy Larbi This should work: arr = ["Bob", "John", "Bobby", "John"] arr.uniq! arr.reject {|a| arr.any? {|b| b != a and b =~ /#{a}/}} Jeremy