Re: Better way to write this
- To: mathgroup at smc.vnet.net
- Subject: [mg85080] Re: Better way to write this
- From: Mark Fisher <particlefilter at gmail.com>
- Date: Sun, 27 Jan 2008 05:47:42 -0500 (EST)
- References: <fnet45$id5$1@smc.vnet.net>
On Jan 26, 4:05 am, Louis Theran <the... at gmail.com> wrote:
> I have the following function that takes as its input a number of
> lists and returns the elements in each of them that appear exactly
> once:
>
> SpecialElements[lists:_List..]:=
> Module[{l,n,Sower,Reaper},
> l = {lists}; n = Length[l];
> Sower[l_,{idx_}]:= Sow[idx,{#}] & /@ l;
> Reaper[val_,{place_}] := Sow[val,place];
> Reaper[val_,x_]:= Null;
> Reap[Reap[MapIndexed[Sower, l],_,Reaper],Range[n]][[2]]]
>
> This works pretty well, but since I am using it on rather large
> inputs, I was wondering if there is a faster way to implement it than
> Reap/Sow, which is sort of counting in unary.
>
> ^L
Hi Louis,
Here's what I'd do. (I don't know how fast it is.)
SpecElem[lists:_List ..] :=
Cases[#, Alternatives @@
Cases[Tally[Join[lists]], {x_, 1} :> x]] & /@ {lists}
--Mark