title: How to remove duplicate key-value pairings in a map
body: <p>I'm stuck on how to transfer key-value pairs from map1 into map2 only if each key has a unique value in map1.</p>
  
  <p>Let's say I have the following maps: </p>
  
  <ul>
  <li>map1: [1,2] [2,4] [4,4]</li>
  <li>map2: [1,2] [2,4]</li>
  </ul>
  
  <p>I suppose the algorithm would be:</p>
  
  <ol>
  <li>Loop through entries in the first map. </li>
  <li>Add a key to map2.</li>
  <li>Add a value to a set which checks against the values of map2</li>
  <li>If the values are duplicate the value doesn't get added to the set and disregard adding its corresponding key to map2.</li>
  </ol>
  
  <p>Code snippet:</p>
  
  <pre><code>public static &lt;K,V&gt; Map&lt;K,V&gt; unique (Map&lt;K,V&gt; m) {
    Map&lt;K,V&gt; newMap = new ArrayMap&lt;K,V&gt;();
  
    //Remember all values in the newMap.
    Set&lt;V&gt; holding = new ArraySet&lt;V&gt;(newMap.values());
  
    for (Map.Entry&lt;K, V&gt; graphEntry : m.entries()) {
       //not sure.
    }
  
    return newMap;  
  }
  </code></pre>
  
  <p>Is my idea of how its supposed to be done on the right track? Quite lost here.</p>