Does anyone know how to improve this for speed performance? The code works fine, but DeleteDuplicates
makes it really slow for large lists.
removeDuplicatesWithRules[mylist_] := Module[{rules, isDuplicateQ},
rules =
Flatten /@ (Subsets[{{1 -> 2, 2 -> 1}, {3 -> 4, 4 -> 3}, {5 -> 6,
6 -> 5}, {7 -> 8, 8 -> 7}, {9 -> 10, 10 -> 9}}]);
(*Function to check if two elements are considered duplicates \
based on rules*)
isDuplicateQ[element1_, element2_] :=
AnyTrue[rules, ((element1 /. #) /.
List :> Composition[Sort, List]) == (element2 /.
List :> Composition[Sort, List]) &];
(*Function to remove duplicates from a list*)
DeleteDuplicates[mylist, isDuplicateQ[#1, #2] &]
];
(*Example test list*)
mylist = {{1, 2}, {2, 1}, {3, 4}, {4, 3}, {5, 6, {7, 8, 2}}, {5,
6, {8, 7, 1}}, {7, 8}, {9, 10}, {10, 9}, {11, 12}};
(*Applying the function*)
removeDuplicatesWithRules[mylist]
(* {{1, 2}, {3, 4}, {5, 6, {7, 8, 2}}, {7, 8}, {9, 10}, {11, 12}} *)
Update:
I didn't check it carefully, and the current solution doesn't provide the correct result in some cases.
For example:
elm1 = {{2}, {4, 5, 8, 9}, {1, 3, 6, 7, 10}};
elm2 = {{1}, {4, 5, 8, 9}, {1, 3, 6, 7, 10}};
makestandard = i_?EvenQ :> (i - 1);
Equal @@ ({elm1, elm2} /. makestandard)
These two elements are not the same based on the list of rules above ((#1 /. a rule) = #2
). makestandard
makes them identical. However, there not exist any rule in rules
that makes (elm1 /. a rule) = elm2
.
removeDuplicatesWithRules
seems to be the main reason for slowing down.DeleteDuplicates[Sort /@ mylist]
produces the same output, and runs much faster. Does that match your need? $\endgroup$#1/. ruleX= #2
whereruleX
could be any rule fromrules
. If you use Sort only, these two elements{5, 6, {7, 8, 2}}, {5, 6, {8, 7, 1}}
would not be duplicates. $\endgroup$DeleteDuplicates[data, sameFunc]
has much worse performance thanDeleteDuplicates[data]
orDeleteDuplicatesBy[data, hashFunc]
in general. $\endgroup$