Scala filter tuples (x, y) == (y, x)

后端 未结 3 966
死守一世寂寞
死守一世寂寞 2021-01-26 13:30

I have a list of tuples, for example:

  (0,2)
  (0,5)
  (2,0)
  (2,5)
  (3,4)
  (4,3)
  (5,0)
  (5,2)

There are some tuples where (x, y)

3条回答
  •  离开以前
    2021-01-26 13:52

    How about first sorting the tuple pairs internally. I believe this technique would pass through your list just twice since distinct is advertised as O(n) complexity:

    val ts = List((0,2),(0,5),(2,0),(2,5),(3,4),(4,3),(5,0),(5,2))
    
    ts.map{ case (x,y) if x < y => (y,x) case (x,y) => (x,y) }.distinct
    

提交回复
热议问题