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}} *)
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$