title: How to remove similar elements from a Dart list?
body: <p>I'd like to remove similar elements from a dart list, where similarity is given by some boolean function.
  For example in Mathematica I would achieve that as follows:</p>
  
  <pre class="lang-mathematica prettyprint-override"><code>Union[{2, -2, 1, 3, 1}, SameTest -&gt; (Abs[#1] == Abs[#2] &amp;)]
  </code></pre>
  
  <p>This statement yields the following list - {-2, 1, 3}. Effectively I'd like to keep one element from each equivalence class.</p>
  
  <p>I know there is a function list.retainWhere( (e) => bool test(e) ), unfortunately this test can only operate on one value at a time. Another option, of course, I could do something like this (just writing from my head)</p>
  
  <pre class="lang-dart prettyprint-override"><code>i=0;
  for(final E in list) {
    i++;      
    for(j=i; j&lt;list.skip(i).length; j++) {
       if sameTest(e, E) then list.removeAt(i+j);
    }  
  }
  </code></pre>
  
  <p>but i find this bit ugly.</p>
  
  <p>Any advice?</p>
  
  <p><strong>UPDATE</strong>
  I will clarify my problem in more details and then show how to solve it using advice given below.</p>
  
  <pre class="lang-dart prettyprint-override"><code>  class Pair&lt;T&gt; {
      final T left;
      final T right;
      Pair(this.left, this.right);    
    }
  </code></pre>
  
  <p>Now I want to have a structure holding such pair or <em>points</em>, and i don't want to hold point which are sufficiently close to each other. To do so I adopt solution of Alexandre Ardhuin and his comment too, which actually makes a difference for more complicated cases: <em>Considering 2 elements <code>e1</code> and <code>e2</code> you have to define <code>hashCode</code> to ensure that <code>e1.hashCode == e2.hashCode</code> if <code>e1 == e2</code></em></p>
  
  <p>so here it goes:</p>
  
  <pre class="lang-dart prettyprint-override"><code>int N=1000;
  
  LinkedHashSet&lt;Pair&lt;double&gt;&gt; myset =
      new LinkedHashSet&lt;Pair&lt;double&gt;&gt;(
        equals: (Pair&lt;double&gt; e1, Pair&lt;double&gt; e2) =&gt;            
          (e1.left - e2.left)*(e1.left - e2.left) + (e1.right - e2.right)*(e1.right - e2.right) &lt; 1/N,
        hashCode: (Pair&lt;double&gt; e){
          int ex = (e.left*N).round();
          int ey = (e.right*N).round();
          return (ex+ey).hashCode;
        }
  );
  
  List&lt;Pair&lt;double&gt;&gt; list = [new Pair&lt;double&gt;(0.1,0.2), new Pair&lt;double&gt;(0.1,0.2001)];
  myset.addAll( list );
  </code></pre>
  
  <p>the result will be <code>{0.1,0.2}</code>. If the second element of <code>list</code> is altered to <code>{0.1, 0.201}</code> I predictably get a set with two elements.</p>
  
  <p>Hope this was useful.</p>