What would be the fastest way to find all cases in a list that match a pattern but without repetition in the result?
Cases[ list, pattern ] // DeleteDuplicates
does the job but is not so efficient since it adds all duplicates to a list and only then deletes them (while not adding them would be faster).
Cases[ DeleteDuplicates @ list, pattern ]
will first delete all duplicates from the list while only the duplicates from the result need to be deleted.
I searched through the documentation of Cases
but could not find any way to turn off repetition.
Edit: A Toy Example
One of the cases I have to deal with is the following. I have a large (>10^6) list of equations in some variables, say { x[1], ..., x[n] }
, and say for example that I would like to use all equalities of the form ( n_?NumericQ == x[i_] | x[i_] == n_?NumericQ ) /; i >= m
to find out some information about the variables x[i]
with i > m
. This could be useful to see whether there's some inconsistency, or maybe some of these values are too big, or not real, etc.
One way to tackle this problem is as follows
(* Small toy example *)
equationList =
{
x[1] == x[3]+x[5],
2 == x[3] x[4],
6 == x[1] x[4],
3 == x[1],
x[5]^2 == x[2]x[3],
x[1] == x[3] + x[5],
3 == x[1],
7 == x[6]
};
infoVars[ m_Integer, list_List ] :=
Cases[
list,
( n_?NumericQ == x[i_] | x[i_] == n_?NumericQ ) /; i >= m :> x[i] -> n
];
infoVars[ 1 , equationList ]
then returns { x[1] -> 3, x[1] -> 3, x[6] -> 7 }
, i.e. a list with duplicate elements.
I could of course apply DeleteDuplicates to this list, but if equationList
contains a lot of duplicate matching expressions, then Mathematica would basically create a long list with multiple duplicates. This is a bit stupid since I have to delete these duplicates anyway so it would be nice if there were an option such that Cases
would not add all those duplicates to the list in the first place.
Deleting duplicates beforehand might also be costly because I would delete duplicates in which I might not be interested.
FirstCase
see help gives the first Subscript[e, i] to match pattern $\endgroup$