# Deleting the same lines from a list

Say I have a list of Lines:

lines = {Line[{{Sqrt[5/8 - Sqrt/8], 1/4 (-1 - Sqrt)}, {Sqrt[
5/8 + Sqrt/8], 1/4 (-1 + Sqrt)}}],
Line[{{Sqrt[5/8 + Sqrt/8], 1/4 (-1 + Sqrt)}, {0, 1}}],
Line[{{0, 1}, {-Sqrt[5/8 + Sqrt/8], 1/4 (-1 + Sqrt)}}],
Line[{{-Sqrt[5/8 + Sqrt/8],
1/4 (-1 + Sqrt)}, {-Sqrt[5/8 - Sqrt/8],
1/4 (-1 - Sqrt)}}],
Line[{{-Sqrt[5/8 - Sqrt/8], 1/4 (-1 - Sqrt)}, {Sqrt[
5/8 - Sqrt/8], 1/4 (-1 - Sqrt)}}],
Line[{{Sqrt[5/8 - Sqrt/8], 1/4 (-1 - Sqrt)}, {0, 1}}],
Line[{{Sqrt[5/8 - Sqrt/8],
1/4 (-1 - Sqrt)}, {-Sqrt[5/8 + Sqrt/8],
1/4 (-1 + Sqrt)}}],
Line[{{Sqrt[5/8 - Sqrt/8],
1/4 (-1 - Sqrt)}, {-Sqrt[5/8 - Sqrt/8],
1/4 (-1 - Sqrt)}}]}


We can see that lines[] and lines[] are the same:

lines[]

Line[{{-Sqrt[5/8 - Sqrt/8], 1/4 (-1 - Sqrt)}, {Sqrt[ 5/8 - Sqrt/8], 1/4 (-1 - Sqrt)}}]

lines[]

Line[{{Sqrt[5/8 - Sqrt/8], 1/4 (-1 - Sqrt)}, {-Sqrt[5/8 - Sqrt/8], 1/4 (-1 - Sqrt)}}]

I tried to use Union and DeleteDuplicates to get rid of a duplicate, but none of them worked. Is it because they can't deal with Line environment? What are the alternatives?

• they are not the same. There is a minus before Sqrt – J42161217 May 15 at 12:02
• the order of the points in each line is different but the points are the same, i.e. the lines are the same – amator2357 May 15 at 12:05

DeleteDuplicatesBy[lines, Sort @@ # &]


or

DeleteDuplicates[Sort @@@ lines]


The key is to realize that we have to Apply Sort one level deeper because of the head Line.

• I see. Thank you @Henrik ! – amator2357 May 15 at 12:18
• You're welcome. – Henrik Schumacher May 15 at 12:19

Another possibility is to use RegionEqual:

Length @ lines
Length @ DeleteDuplicates[lines, RegionEqual]


8

7

If your lines had more than 2 segments, than using Sort to canonicalize wouldn't work.

It is because the arguments of the 5-th and 8-th terms go in different order. Indeed, let us take only the arguments:

arg = lines[[All, 1]];


and let us try to compare the arguments of the 5th and 8th terms:

arg[] === arg[]

(*  False  *)


Now let us sort the arguments:

newArg = Sort /@ lines[[All, 1]]


and let us check the 5th and 8th terms

newArg[] === newArg[]

(*  True  *)


Now the duplicates will be deleted, and here is the result:

Line /@ DeleteDuplicates[newArg]


Have fun!

• Nice! Thank you! – amator2357 May 15 at 12:23