title: How can I compute the intersection between two arrays that may contain duplicate elements?
body: <p>How can I compute the intersection (the common elements) of two arrays? I tried with this code: </p>
  
  <pre><code>my @array1 = (1, 2, 3, 3, 3, 3, 4);
  my @array2 = (2, 3, 4, 4, 4);
  my %original = ();
  my @isect = ();
  
  map { $original{$_} = 1 } @array1;
  @isect = grep { $original{$_} } @array2;
  
  print "@isect\n";
  </code></pre>
  
  <p>The result is <code>2 3 4 4 4</code>, but I want to get <code>2 3 4</code>.</p>
  
  <p>Another example: </p>
  
  <pre><code>@array1 = (5, 6, 7, 7, 7, 1, 4);
  @array2 = (5, 6, 7, 6, 7, 7, 4, 4, 4);
  </code></pre>
  
  <p>This should return <code>5 6 7 7 7 4</code>, not <code>5 6 7 6 7 7 4 4 4</code>. How can I do this?</p>