in reply to Re^2: Subtracting Lists
in thread Subtracting Lists

The O.P did not state if he wanted the results in the same order as the original array.

Even without that requirement, if the original array contained duplicate values, a solution that collapsed the original into a hash would eliminate duplicates, and , IMHO, provide a wrong result.

However, 3 good solutions which do not collapse the original array have been provided by others, below.

I would suggest that educated_foo's (++) is the most portable,readable one with no dependencies.

     "A fanatic is one who redoubles his effort when he has forgotten his aim."—George Santayana

Replies are listed 'Best First'.
Re^4: Subtracting Lists
by ikegami (Patriarch) on Jun 02, 2008 at 06:12 UTC

    I would suggest that educated_foo's (++) is the most portable,readable one with no dependencies.

    That isn't the OP's criteria ("I am trying to find the fastest (not necessarily most elegant) way"). I think both his and mine are similarly fast, however.

    I specifically avoided undef @h{}; for being cryptic.

    I did appreciate the combining the extraction of the result into the operation itself, but it's less modular (i.e. complicates things if you want to work with the resulting set).

      I specifically avoided undef @h{}; for being cryptic.
      That's at least part of the reason I used it ;). It's a weird idiom, but useful when you want to use hashes as sets rather than maps, so I think more people should be introduced to it. Besides, it creates fewer scalars and executes fewer operations than other methods, and the OP did say "fastest". That said, I like your solution (to a slightly different problem) -- simple, terse, and no bizarro modules.
Re^4: Subtracting Lists
by ikegami (Patriarch) on Jun 02, 2008 at 06:03 UTC
    By definition, sets have no duplicates.

    Update: Hum, I guess he didn't use the word "set", but "difference" is an operation that produces a set.

      You are correct in the statement that sets have distinct elements.

      But the O.P did not say he was dealing with sets - he said "arrays".

      Update: Um - Ignore this post, since you updated yours ...

           "A fanatic is one who redoubles his effort when he has forgotten his aim."—George Santayana

      Update: Hum, I guess he didn't use the word "set", but "difference" is an operation that produces a set.

      I personally believe that a notion of difference can be given for the mathematical objects representing both ordered lists, i.e. functions from some finite set (0...n-1) into some other set, and unordered ones, i.e. classes of equivalence of those functions wrt composition with a permutation on the domain. Except that at least two reasonable definitions of such a difference spring to mind, abstractly, and I think that with no further specification one just has to pick the one that seems most reasonable for the actual problem. In the example the OP gave, both would do, so there's an ambiguity.

      --
      If you can't understand the incipit, then please check the IPB Campaign.