Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Possible Duplicate:
How can I remove B -> A from a list if A -> B is in the list?

I'm working toward building a graph and use some code to construct my rules which results in a form as follows:

g = {1 -> 2, 1 -> 3, 2 -> 1, 2 -> 3, 2 -> 4, 3 -> 2, 3 -> 4, 3 -> 1, 4 -> 3, 4 -> 2}

I would like to delete all reverse duplicate entries form the list, i.e. both 1->2 and 2->1 appear in the list. I would like to get rid of one of them and do this for all reverse duplicates in the list g.

share|improve this question

marked as duplicate by Mr.Wizard Jun 27 '12 at 20:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
All: please try a bit harder to find duplicate questions; thank you. –  Mr.Wizard Jun 27 '12 at 20:46
    
@Tom That case is already covered by my own answer to the previous question. If you feel that this closure was in error feel free to vote to reopen. IMHO the fact that identical answers were posted indicates it is a true duplicate. (ps. please use the @name notification when responding; I didn't see your message until now.) –  Mr.Wizard Jun 28 '12 at 7:10

2 Answers 2

up vote 4 down vote accepted

This satisfies your needs

DeleteDuplicates @ (Sort /@ g)
{1 -> 2, 1 -> 3, 2 -> 3, 2 -> 4, 3 -> 4}
share|improve this answer
1  
Kind of a waste to f @ (stuff) when you can just f[stuff], no? –  J. M. Jun 27 '12 at 16:52
    
Well, no unnecessary signs : Sort /@ g // DeleteDuplicates –  Artes Jun 27 '12 at 16:57
1  
@J.M. I also use this kind of @(...) syntax because of reasons given here. By removing the head you don't have to jump to the end to remove ]. –  István Zachar Jun 28 '12 at 8:53
    
@István: still bloody ugly for me. To each his own, I suppose. –  J. M. Jun 28 '12 at 14:21

Union[Sort /@ g] seems to be what you need...

share|improve this answer

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