# Union sorting after ignoring some digits

I have a list with its bellow elements:

{-0.7071067811865476, -0.7071067811865475, 0.7071067811865475, 0.7071067811865476}


If I use Union, but there is no change. Because the numbers are different. I want to use of Union for keeping just -0.707 and 0.707 and other digits after third (I mean ...10678....) are not important. How can I apply this aim in Union function. Is there another function for this goal?

• Use Round before or put it as a test in DeleteDuplicates. – Kuba Jul 7 '15 at 19:08
• Union has the option SameTest: Union[list, SameTest -> (Abs[#1 - #2] < 10^-4 &)] – Bob Hanlon Jul 7 '15 at 19:16
• With thanks to Kuba, but I think the second comment is more flexible. I have 200 lists, all of them are not similar and if I choose "round" I just have to select a number for rounding which is not useful for other lists – Unbelievable Jul 7 '15 at 19:24
• A comment in favour of rounding: using the SameTest option will slow down Union. Without SameTest, Union relies on sorting, which is of $O(n \log n)$ complexity. SameTest forces comparing each pair, i.e. $O(n^2)$ complexity. – Szabolcs Jul 7 '15 at 19:53
• Alternatively, Sort[Mean /@ FindClusters[list]] – Bob Hanlon Jul 8 '15 at 15:22

Try the following shory code:

DeleteDuplicatesBy[lst,Floor[#,10^-4]&]


Will this help?

Therefore, by all means, study all the answers in the comments assiduously. The Example @Daniel Lichtblau I like most.

lst = {-0.7071067811865476, -0.7071067811865475, 0.7071067811865475, 0.7071067811865476}

Rationalize[lst, 0.001]


$\left\{-\frac{29}{41},-\frac{29}{41},\frac{29}{41},\frac{29}{41}\right\}$

DeleteDuplicates[{-(29/41), -(29/41), 29/41, 29/41}]


$\left\{-\frac{29}{41},\frac{29}{41}\right\}$

• Will DeleteDuplicatesBy be a better solution? – Wjx Jun 27 '16 at 15:24