From: Amos King Date: 2008-09-17T23:21:19+09:00 Subject: Re: array question That only works if they are touching and that can be accomplished with inject really easy. ([1,2,2,3]+[1,4,7,7]).sort..inject([]) { |final, e| fianl[-1] == e? final[0..-2] : final << e } #=> [3,4] ([1,2,3]+[1,4,7]).sort..inject([]) { |final, e| fianl[-1] == e? final[0..-2] : final << e } #=> [2,3,4,7] although with the later one you can just do ([1,2,3]+[1,4,7]).uniq On Tue, Sep 16, 2008 at 10:08 AM, Tod Beardsley wrote: > Case 1 and 2, maybe the desired result? > > irb(main):088:0> def there_can_be_only_one(arr) > irb(main):089:1> arr.sort!.collect {|x| (arr[arr.index(x)] != > arr[arr.index(x)+1] ? x:nil)}.compact > irb(main):090:1> end > => nil > irb(main):091:0> there_can_be_only_one([1,2,3] + [1,4,7]) > => [2, 3, 4, 7] > irb(main):092:0> there_can_be_only_one([1,2,2,3] + [1,4,7,7]) > => [3, 4] > irb(main):093:0> > > ...assumes the elements to be compared are all the same type (Fixnums > can't sort with Strings, eg) > > On Tue, Sep 16, 2008 at 9:29 AM, Nathan Powell wrote: >> On Tue, Sep 16, 2008 at 11:25:00PM +0900, Amos King wrote: >>> Forgot to ask what he was looking for. >> >> In the original email: >> >> >> For example, array1=[1,2,3],array2=[1,4,7] >> the return array is [2,3,4,7] >> >> >> It's not entirely clear as one could arrive at that final array a couple >> ways. >> >> -- >> nathan >> nathan_at_nathanpowell_dot_org >> >> At this point, anyone proposing to run Windows on servers should be prepared >> to explain what they know about servers that Google, Yahoo, and Amazon don't. >> ~ Paul Graham >> ------------------------------------ >> >> > > > > -- > todb@planb-security.net | ICQ: 335082155 | Note: Due to Google's > privacy policy and the United States' > policy on electronic surveillance , > please do not IM/e-mail me anything you wish to remain secret. > > -- Amos King A. King Software Development and Consulting, L.C. http://dirtyInformation.com -- Looking for something to do? Visit http://ImThere.com