Re: List complement operator
- To: mathgroup at smc.vnet.net
- Subject: [mg84983] Re: [mg84950] List complement operator
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Tue, 22 Jan 2008 01:48:52 -0500 (EST)
- Reply-to: hanlonr at cox.net
a = {1, 1, 1, 2, 3, 3, 4, 5, 5, 5, 2, 6, 7, 7}; b = {1, 2, 2, 3, 4, 7, 8}; Fold[Delete[#1, Position[#1, #2][[1, 1]]] &, a, Fold[DeleteCases[#1, #2] &, b, Complement[b, a]]] {1,1,3,5,5,5,6,7} Bob Hanlon ---- zac <replicatorzed at gmail.com> wrote: > Dear group, > > I'm loooking for an elegant and efficient way to subtract a list (not > a set) from an other one. > > a = {1, 1, 1, 2, 3, 3, 4, 5, 5, 5, 2, 6, 7, 7}; > b = {1, 2, 2, 3, 4, 7, 8}; > > Complement[a,b] returns {5, 6}, as expected. However I would like to > treat each entry as a discrete element, therefore my target result is: > {1, 1, 3, 5, 5, 5, 6, 7}. Union can not be used, although Sort is > allowed (ie. whether the result is sorted or not is of no concern). > Any idea? > > Istvan Zachar >