Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Removing duplicates and substrings from an array

Reply
Thread Tools

Removing duplicates and substrings from an array

 
 
Sam Larbi
Guest
Posts: n/a
 
      11-26-2007
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

 
Reply With Quote
 
 
 
 
Siep Korteling
Guest
Posts: n/a
 
      11-26-2007
Sam Larbi wrote:
> 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"]
>


["Bob", "John", "Bobby", "John"].uniq!

(or uniq )
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Shairon Toledo
Guest
Posts: n/a
 
      11-26-2007
Sam Larbi wrote:
> 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



You tried to use the method uniq?
<code>
[1,2,3,4,1,3].uniq => [1,2,3,4]
</code>
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Marc Heiler
Guest
Posts: n/a
 
      11-26-2007
I think there could also be a .map solution but I cant figure it out
right now, .uniq just really seems the most simple and elegant for this
given problem at hand
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
yermej
Guest
Posts: n/a
 
      11-26-2007
On Nov 26, 9:15 am, Sam Larbi <sla...@gmail.com> 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
 
Reply With Quote
 
Lionel Bouton
Guest
Posts: n/a
 
      11-26-2007
yermej wrote the following on 26.11.2007 18:15 :
> On Nov 26, 9:15 am, Sam Larbi <sla...@gmail.com> 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

 
Reply With Quote
 
Lionel Bouton
Guest
Posts: n/a
 
      11-26-2007
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 <sla...@gmail.com> 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

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      11-26-2007
Lionel Bouton wrote:
> arr.reject {|a| arr.any? {|b| b != a and a.index(b) } }


I'd make that index into include? because you don't really care about the
index here.


--
Jabber:
ICQ: 205544826

 
Reply With Quote
 
yermej
Guest
Posts: n/a
 
      11-26-2007
On Nov 26, 11:20 am, Lionel Bouton <lionel-subscript...@bouton.name>
wrote:
> yermej wrote the following on 26.11.2007 18:15 :
>
>
>
> > On Nov 26, 9:15 am, Sam Larbi <sla...@gmail.com> 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
 
Reply With Quote
 
Lionel Bouton
Guest
Posts: n/a
 
      11-26-2007
Sebastian Hungerecker wrote the following on 26.11.2007 18:27 :
> Lionel Bouton wrote:
>
>> arr.reject {|a| arr.any? {|b| b != a and a.index(b) } }
>>

>
> I'd make that index into include? because you don't really care about the
> index here.
>


I agree, the code is then easier to read too.

Lionel

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Q] removing array duplicates where a subset is unique Chuck Remes Ruby 23 07-20-2009 03:21 AM
Sorting and removing duplicates Shane Java 8 04-23-2007 10:17 PM
identifying the duplicate when removing duplicates from any array Jack Perl Misc 1 06-10-2006 08:11 PM
removing duplicates from a string array Fred Java 15 03-12-2005 12:32 AM
array - removing duplicates Jerry Preston Perl Misc 5 11-15-2004 02:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57