From: Lionel Bouton Date: 2007-11-27T02:25:03+09:00 Subject: Re: Removing duplicates and substrings from an array Lionel Bouton wrote the following on 26.11.2007 18:20 : > 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) } } > Oups: I misread the question. It should be b.index(a) (I rejected the superstrings instead of the substrings). Lionel