6
$\begingroup$

I'm wondering if this is possible to delete duplicate elements if a=b and b=a using deleteduplicates or any simple way.

l1 = {{284, 220}, {220, 284}, {1210, 1184}, {1184, 1210}, {2924, 
  2620}, {2620, 2924}, {5564, 5020}, {5020, 5564}, {6368, 
  6232}, {6232, 6368}, {10856, 10744}, {10744, 10856}, {14595, 12285}}

DeleteDuplicatesBy[
l1], First == Last && Last==First] 

For exemple, I would like to delete {284,220}, {1210,1184}, ...

Thank you

$\endgroup$
1
  • 1
    $\begingroup$ l1 // DeleteDuplicatesBy[Sort] $\endgroup$ Dec 15, 2022 at 14:33

4 Answers 4

5
$\begingroup$

Try this:

DeleteDuplicates[Sort /@ l1]
(*{{220, 284}, {1184, 1210}, {2620, 2924}, {5020, 5564}, {6232, 6368}, {10744, 10856}, {12285, 14595}}*)
$\endgroup$
5
  • 1
    $\begingroup$ It's a (+1) from me. Just as a comment one could use Union, i.e DeleteDuplicates[Union /@ l1] $\endgroup$
    – bmf
    Dec 15, 2022 at 1:29
  • 1
    $\begingroup$ Hi, friend! I'm glad to see you here. I hope everything went well with your move. You're right, it works great with Union. $\endgroup$ Dec 15, 2022 at 1:32
  • 1
    $\begingroup$ Hello.. Still trying to figure out some stuff, but it's getting better on a daily basis, thanks! Hope you're also well :-) $\endgroup$
    – bmf
    Dec 15, 2022 at 1:33
  • 1
    $\begingroup$ I'm coming out of a flu, but everything is going well so far, thanks! :-) $\endgroup$ Dec 15, 2022 at 1:36
  • 1
    $\begingroup$ Thank you! I can't believe I didn't think of it. $\endgroup$ Dec 15, 2022 at 1:48
6
$\begingroup$

Ok, since @E. Chan-López gave the proper answer let's see with what else we can come up.

Suggested solution

Gather[Sort /@ l1][[All, 1]]

Comparison with the DeleteDuplicates solution

DeleteDuplicates[Sort /@ l1] - Gather[Sort /@ l1][[All, 1]]

{{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}

$\endgroup$
4
$\begingroup$
l1 = {{284, 220}, {220, 284}, {1210, 1184}, {1184, 1210}, {2924, 
   2620}, {2620, 2924}, {5564, 5020}, {5020, 5564}, {6368, 
   6232}, {6232, 6368}, {10856, 10744}, {10744, 10856}, {14595, 
   12285}};

ConnectedComponents[Graph[UndirectedEdge @@@ l1]]
{{5020, 5564}, {6368, 6232}, {14595, 12285}, {2620, 2924}, {220, 
  284}, {10856, 10744}, {1184, 1210}}
$\endgroup$
4
$\begingroup$
list = {{12, 10}, {10, 12}, {20, 20}, {20, 20}, {16, 15}, {12, 10}};

I would suggest

DeleteDuplicatesBy[SymmetricDifference] @ list

{{12, 10}, {20, 20}, {16, 15}}

because it doesn't sort the sublists. "Why should the unpaired {16, 15} become {15, 16}"? one could ask pedantically.

Compare with the accepted answer:

DeleteDuplicates[Sort /@ list]

{{10, 12}, {20, 20}, {15, 16}}

SymmetricDifference came with V 13.1.

$\endgroup$
1
  • $\begingroup$ (+1) An important point IMO. $\endgroup$
    – user1066
    Sep 26 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.