From: yermej Date: 2007-11-27T02:39:59+09:00 Subject: Re: Removing duplicates and substrings from an array On Nov 26, 11:20 am, Lionel Bouton wrote: > yermej wrote the following on 26.11.2007 18:15 : > > > > > 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"] > > This should work: > > > arr = ["Bob", "John", "Bobby", "John"] > > arr.uniq! > > arr.reject {|a| arr.any? {|b| b != a and b =~ /#{a}/}} > > You'll have surprises if there's a "." element... > > arr = ["Bob", "John", "Bobby", "John"] > arr.uniq! > arr.reject {|a| arr.any? {|b| b != a and a.index(b) } } > > seems safer and quicker to me. > > Lionel Good point. Thank you. Jeremy