<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>array element comparison &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/array-element-comparison/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Fri, 17 Jun 2022 22:01:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Compare all elements in an array against each other in Java</title>
		<link>https://codepills.com/compare-all-elements-in-an-array-against-each-other-in-java/</link>
					<comments>https://codepills.com/compare-all-elements-in-an-array-against-each-other-in-java/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Sun, 08 Nov 2020 10:08:03 +0000</pubDate>
				<category><![CDATA[Solutions]]></category>
		<category><![CDATA[array element comparison]]></category>
		<category><![CDATA[Java]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1075</guid>

					<description><![CDATA[One of the most common requests when processing an array is to compare each element against all other elements in the array. An abstract solution to this request is demonstrated in this article in Java <a href="https://codepills.com/compare-all-elements-in-an-array-against-each-other-in-java/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>One of the most common requests when processing an array is to compare each element in an array against every other element. You can imagine it as you want to take the 1st array element and compare it with every other element, then take the 2nd array element and compare it with every other element and so on and on.</p>
<p><span id="more-1075"></span></p>
<p>We will write the following code examples in Java. As input example, let&#8217;s have a simple Java array <code>array</code> with 5 integer elements as method input for our <code>arrayComparison(final int[] arr)</code>. Here is the Java code:</p>
<pre><code class="language-java">final int[] array = new int[]{1, 2, 3, 4, 5};
public void arrayComparison(final int[] arr);</code></pre>
<p>So let&#8217;s write a simple solution for all element comparison. There is no other way than just loop trough array and take each element individually and compare it with the rest of an array. Here is the code which does exactly that:</p>
<pre><code class="language-java">for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr.length; j++) {
        // Comparison code
        System.out.println(arr[i] + " " + arr[j]);
    }
}</code></pre>
<p>And this is the Java code outcome:</p>
<pre><code class="language-bash">1 - 1   2 - 1   3 - 1   4 - 1   5 - 1
1 - 2   2 - 2   3 - 2   4 - 2   5 - 2
1 - 3   2 - 3   3 - 3   4 - 3   5 - 3
1 - 4   2 - 4   3 - 4   4 - 4   5 - 4
1 - 5   2 - 5   3 - 5   4 - 5   5 - 5</code></pre>
<p>From the output, we see that we compare every element with every other element in the array.</p>
<p>Is it the most effective solution we can come with? Definitely no! Looping through elements <code>2 - 3</code> is the same as looping through <code>3 - 2</code>, isn't it?</p>
<p>We can slightly optimize this solution by making a check on every iteration and omitting self-comparing elements. Here is the improved code:</p>
<pre><code class="language-java">for (int i = 0; i < arr.length; i++) {
    for (int j = i; j < arr.length; j++) {
        // Comparison code
        System.out.println(arr[i] + " " + arr[j]);
    }
}</code></pre>
<p>And this is overall Java code output:</p>
<pre><code class="language-bash">1 - 1
1 - 2   2 - 2
1 - 3   2 - 3   3 - 3
1 - 4   2 - 4   3 - 4   4 - 4
1 - 5   2 - 5   3 - 5   4 - 5   5 - 5</code></pre>
<p>In this solution, we see that we do not need to compare the same elements, for example, <code>1 - 1</code> etc.</p>
<p>We can push optimization again little bit further and by making a check on every iteration omit self-reflecting elements. Here is more optimized code:</p>
<pre><code class="language-java">for (int i = 0; i < arr.length; i++) {
    for (int j = i; j < arr.length; j++) {
        if(arr[i] != arr[j]) {
            // Comparison code
            System.out.println(arr[i] + " " + arr[j]);
        }
    }
}</code></pre>
<p>And this is overall Java code output:</p>
<pre><code class="language-bash">
1 - 2
1 - 3   2 - 3
1 - 4   2 - 4   3 - 4
1 - 5   2 - 5   3 - 5   4 - 5</code></pre>
<p>This way, we get precisely the comparison pairs we were looking for.</p>
<p>However, there is still a way how to optimize this solution. First, we still loop through the identical elements; but we omit them with the  <code>if</code> check.</p>
<p>Is there a way how to rewrite and optimize this solution? Yes, it is!</p>
<p>We can improve the existing solution with the start of comparison from the point of element one after the current point on the second level iteration.</p>
<p>Here is the solution:</p>
<pre><code class="language-java">for (int i = 0; i < arr.length; i++) {
    for (int j = i +  1; j < arr.length; j++) {
        // Comparison code
        System.out.println(arr[i] + " " + arr[j]);
    }
}</code></pre>
<p>And this is overall Java code output:</p>
<pre><code class="language-bash">
1 - 2
1 - 3   2 - 3
1 - 4   2 - 4   3 - 4
1 - 5   2 - 5   3 - 5   4 - 5</code></pre>
<p><b>Note:</b> Real trick of this solution is on its last iteration. When we finish <code>3 - 4</code> we jump to <code>4</code> element. However inner loop is <code>5 = arr.length</code> therefore is not valid, loop terminates and and no print is pushed to System output.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/compare-all-elements-in-an-array-against-each-other-in-java/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
