<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Intersection between 2 arrays]]></title><description><![CDATA[<p>hi,</p>
<p>sind die laufzeit analysen beider algorithmen korrekt?</p>
<pre><code>// Overall Complexity: O(n + m)
vector&lt;int&gt; get_intersection_sorted(vector&lt;int&gt; &amp;arr1, vector&lt;int&gt; &amp;arr2) {
	// we assume the arrays to be pre-sorted
	typedef std::unordered_map&lt;int, int&gt; unordered_map;
	unordered_map ht;
	unordered_map::iterator it;
	std::vector&lt;int&gt; out;

	for(int i = 0; i &lt; arr1.size(); i++) {
		ht[arr1[i&rsqb;&rsqb;++; // O(n) ... n = length of arr1
	}

	for(int i = 0; i &lt; arr2.size(); i++) { // O(m) ... m = length of arr2
		it = ht.find(arr2[i]);  // O(1)
		if(it != ht.end()) {
			out.push_back(arr2[i]);
			(*it).second -= 1;
		}
	}

	return out;
}

// Overall Complexity: O(n log n + m * log n)
vector&lt;int&gt; get_intersection_unsorted(vector&lt;int&gt; &amp;arr1, vector&lt;int&gt; &amp;arr2) {
	// we assume the arrays to be non sorted
	std::sort(arr1.begin(), arr1.end()); // O(n log n) ... n = length of arr1 ... i assume this to be quicksort!?
	std::vector&lt;int&gt; out;

	for(int i = 0; i &lt; arr2.size(); i++) {
		if(std::binary_search(arr1.begin(), arr1.end(), arr2[i])) { // O(m * log n) ... m = length of arr2
			out.push_back(arr2[i]);
		}
	}

	return out;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/314058/intersection-between-2-arrays</link><generator>RSS for Node</generator><lastBuildDate>Sat, 01 Aug 2026 15:50:52 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/314058.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 19 Feb 2013 11:19:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Intersection between 2 arrays on Tue, 19 Feb 2013 11:19:41 GMT]]></title><description><![CDATA[<p>hi,</p>
<p>sind die laufzeit analysen beider algorithmen korrekt?</p>
<pre><code>// Overall Complexity: O(n + m)
vector&lt;int&gt; get_intersection_sorted(vector&lt;int&gt; &amp;arr1, vector&lt;int&gt; &amp;arr2) {
	// we assume the arrays to be pre-sorted
	typedef std::unordered_map&lt;int, int&gt; unordered_map;
	unordered_map ht;
	unordered_map::iterator it;
	std::vector&lt;int&gt; out;

	for(int i = 0; i &lt; arr1.size(); i++) {
		ht[arr1[i&rsqb;&rsqb;++; // O(n) ... n = length of arr1
	}

	for(int i = 0; i &lt; arr2.size(); i++) { // O(m) ... m = length of arr2
		it = ht.find(arr2[i]);  // O(1)
		if(it != ht.end()) {
			out.push_back(arr2[i]);
			(*it).second -= 1;
		}
	}

	return out;
}

// Overall Complexity: O(n log n + m * log n)
vector&lt;int&gt; get_intersection_unsorted(vector&lt;int&gt; &amp;arr1, vector&lt;int&gt; &amp;arr2) {
	// we assume the arrays to be non sorted
	std::sort(arr1.begin(), arr1.end()); // O(n log n) ... n = length of arr1 ... i assume this to be quicksort!?
	std::vector&lt;int&gt; out;

	for(int i = 0; i &lt; arr2.size(); i++) {
		if(std::binary_search(arr1.begin(), arr1.end(), arr2[i])) { // O(m * log n) ... m = length of arr2
			out.push_back(arr2[i]);
		}
	}

	return out;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2299898</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299898</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Tue, 19 Feb 2013 11:19:41 GMT</pubDate></item><item><title><![CDATA[Reply to Intersection between 2 arrays on Tue, 19 Feb 2013 11:24:09 GMT]]></title><description><![CDATA[<p>der erste algo ist auch für non-sorted arrays <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2299899</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299899</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Tue, 19 Feb 2013 11:24:09 GMT</pubDate></item><item><title><![CDATA[Reply to Intersection between 2 arrays on Tue, 19 Feb 2013 14:38:47 GMT]]></title><description><![CDATA[<p>Jo, die Komplexitäten halte ich für richtig. Daß der naheliegende Algorithmus nicht dabei ist, ist Absicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2299954</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299954</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Tue, 19 Feb 2013 14:38:47 GMT</pubDate></item><item><title><![CDATA[Reply to Intersection between 2 arrays on Tue, 19 Feb 2013 14:41:49 GMT]]></title><description><![CDATA[<p>inplace algorithmus?</p>
<pre><code>// Overall Complexity: O(n log n + m * log n)
void get_intersection_unsorted(vector&lt;int&gt; &amp;arr1, vector&lt;int&gt; &amp;arr2) {
	// we assume the arrays to be non sorted
	std::sort(arr1.begin(), arr1.end()); // O(n log n) ... n = length of arr1
	std::vector&lt;int&gt;::iterator it = arr2.begin();

	while(it != arr2.end()) {
		if(!std::binary_search(arr1.begin(), arr1.end(), *it)) { // O(m * log n) ... m = length of arr2
			it = arr2.erase(it);
		}
		else {
			it++;
		}
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2299955</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299955</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Tue, 19 Feb 2013 14:41:49 GMT</pubDate></item><item><title><![CDATA[Reply to Intersection between 2 arrays on Tue, 19 Feb 2013 14:55:43 GMT]]></title><description><![CDATA[<p>geri1 schrieb:</p>
<blockquote>
<p>inplace algorithmus?</p>
</blockquote>
<p>Nö. mergen.</p>
<pre><code>while(
  if(*a&lt;*b)
    ++a;
  else if(*b&lt;*a)
    ++b;
  else{
    outpush(*a);
    ++a,++b;
  }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2299960</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299960</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Tue, 19 Feb 2013 14:55:43 GMT</pubDate></item><item><title><![CDATA[Reply to Intersection between 2 arrays on Wed, 20 Feb 2013 00:30:25 GMT]]></title><description><![CDATA[<p>ist aber langsamer als binary sort!?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2300129</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300129</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Wed, 20 Feb 2013 00:30:25 GMT</pubDate></item><item><title><![CDATA[Reply to Intersection between 2 arrays on Wed, 20 Feb 2013 00:36:18 GMT]]></title><description><![CDATA[<p>geri1 schrieb:</p>
<blockquote>
<p>ist aber langsamer als binary sort!?</p>
</blockquote>
<p>nö, ist offensichtlich schnell.</p>
<p>edit: binary sort?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2300131</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300131</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Wed, 20 Feb 2013 00:36:18 GMT</pubDate></item><item><title><![CDATA[Reply to Intersection between 2 arrays on Thu, 21 Feb 2013 14:31:40 GMT]]></title><description><![CDATA[<pre><code>meinem algo ist: O(n log n + m * log n) 

Volkard: O(n) ?
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2300624</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2300624</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Thu, 21 Feb 2013 14:31:40 GMT</pubDate></item></channel></rss>