Note that there are some explanatory texts on larger screens.

plurals
  1. POMerge sort remove duplicates
    primarykey
    data
    text
    <p>I am trying to sort an array via <strong>merge sort</strong>, and while sorting, remove elements that I have deemed equal. I am recursively calling merge sort and then merging. </p> <p>I get to this point and find that <code>a</code> and <code>c</code> are <strong>duplicates</strong>. </p> <pre><code>a b | c d </code></pre> <p>I determine which one I want based on certain criteria and I pick c. I increment the right hand counter and and the left hand counter and compare b and d. Say I pick d, then I pick b. I want my final list to only have the elements </p> <pre><code>c d b </code></pre> <p>However, what is happening is on the next recursive call, <code>start</code> and <code>end</code> are 0 and 3 so d is listed twice in the array on the next call. The array that the merge procedure works with is:</p> <pre><code>c d b d </code></pre> <p>Here is the code. Thanks in advance.</p> <pre><code>private static void merge(int[] data, int start, int mid, int end) { int firstCopied=0; int secondCopied=0; int index=0; int length=end-start+1; int[] temp = new int[end-start+1]; int firstSize=mid-start+1; int secondSize=end-mid; while(firstCopied &lt; firstSize &amp;&amp; secondCopied &lt; secondSize) { if(data[start+firstCopied] &lt; data[mid+1+secondCopied]) { temp[index++] = data[start+firstCopied]; firstCopied++; } else if(data[start+firstCopied] &gt; data[mid+1+secondCopied]) { temp[index++] = data[mid+1+secondCopied]; secondCopied++; } else if(data[start+firstCopied]==data[mid+1+secondCopied]) { boolean result = PickOne(); if(result) { temp[index++] = data[start+firstCopied]; } else { temp[index++] = data[mid+1+secondCopied]; } firstCopied++; secondCopied++; length--; } } while(firstCopied &lt; firstSize) { temp[index++] = data[start+firstCopied]; firstCopied++; } while(secondCopied &lt; secondSize) { temp[index++] = data[mid+1+secondCopied]; secondCopied++; } for(int i=0; i&lt;length; i++) { data[start+i]=temp[i]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload