This method is slightly evil.
g[input_List] := Module[{f},
f[item_] := {Sow[item, "Once"]; f[item] := Sow[item, "Twice"]};
Complement @@ Reap[f /@ input][[2]]
]
There should be a faster way to extract the "only once defined f
s" using Select[] and DownValues[f] instead of Sow/Reap/Complement, but I'm too tired to figure that out right now. (I.e., the solution should look like
g[input_List] := Module[{f},
f[item_] := f[item] := f[item] := {};
Select[Downvalues[f], (* something tricky *)]
]
.)
Edit 13 October 2015: @march's comments led me to revisit this.
g[input_List] := Module[{f},
f[item_] := f[item] := f[item] := {};
f /@ input;
(* To see what's going on, uncomment the next line. *)
(* Print[DownValues[f]; *)
Select[DownValues[f],
And[#[[2]] === Null, #[[1, 1, 1]] =!= item$_] &
][[All, 1, 1, 1]]
]
This uses delayed evaluation to re-define f
the first and second times an expression is its argument from "something that is not {}" to "{}", then scan through the downvalues of f
finding the arguments for which it is still defined to be "something that is not {}".
Why would one do this? It's related to an old hack for RemoveDuplicates[] from before that function existed:
remDupes[x_] := Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ x]
(We can't do this directly for the OP's problem because the second time we see an input we can't travel back in time undo the output of it the first time we saw it. We have to wait until the end of the list to determine which inputs were never seen a second time.)
We expect this function to run in time $O(n \log n)$ for $n$ the length of the input, where manipulation of the items in the list is assumed to take constant time. (It's really $O(n \log n + m)$ where $m$ is the length of the output, but the output is not longer than the input...)