Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following Python 3 code will remove duplicates from a list (array). It does this by scanning the array from start towards end and compares the target element with the element one larger. If they are the same they are removed. If the element pointer is not pointing at 0, then it is reduced by 1 in order to catch nested pairs. If the two compared elements are different then the pointer is incremented.</p> <p>I'm sure there's a more pythonic way to remove two adjacent elements from a list, but I'm new to Python and haven't figured that out yet. Also, you'll want to get rid of the <code>print(indx, SampleArray)</code> statement--I left it in there to let you follow the progress in the output listing below.</p> <pre><code># Algorithm to remove duplicates in a semi-sorted list def CompressArray(SampleArray): indx=0 while(indx &lt; len(SampleArray)-1): print(indx, SampleArray) if(SampleArray[indx]==SampleArray[indx+1]): del(SampleArray[indx]) del(SampleArray[indx]) if(indx&gt;0): indx-=1 else: indx+=1 return SampleArray </code></pre> <p>Here are sample runs for:</p> <ul> <li>[1, 2, 2, 3, 4]</li> <li>[1, 2, 2, 3, 4, 4, 3, 5]</li> <li>[1, 2, 2, 3, 3, 3, 3, 4, 3, 3, 5, 6, 7, 8, 8]</li> <li>[1, 2, 2, 3, 4, 6, 7, 7, 6, 4, 3, 8, 8, 5, 9, 10, 10, 9, 11]</li> <li>[1, 1, 2, 3, 3, 2, 4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9]</li> </ul> <pre><code> ================================ 0 [1, 2, 2, 3, 4] 1 [1, 2, 2, 3, 4] 0 [1, 3, 4] 1 [1, 3, 4] [1, 3, 4] ================================ 0 [1, 2, 2, 3, 4, 4, 3, 5] 1 [1, 2, 2, 3, 4, 4, 3, 5] 0 [1, 3, 4, 4, 3, 5] 1 [1, 3, 4, 4, 3, 5] 2 [1, 3, 4, 4, 3, 5] 1 [1, 3, 3, 5] 0 [1, 5] [1, 5] ================================ 0 [1, 2, 2, 3, 3, 3, 3, 4, 3, 3, 5, 6, 7, 8, 8] 1 [1, 2, 2, 3, 3, 3, 3, 4, 3, 3, 5, 6, 7, 8, 8] 0 [1, 3, 3, 3, 3, 4, 3, 3, 5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, 11] 2 [1, 5, 9, 9, 11] 1 [1, 5, 11] [1, 5, 11] ================================ 0 [1, 1, 2, 3, 3, 2, 4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9] 0 [2, 3, 3, 2, 4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9] 1 [2, 3, 3, 2, 4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9] 0 [2, 2, 4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9] 0 [4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9] 1 [4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9] 2 [4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9] 1 [4, 5, 5, 7, 8, 8, 7, 4, 9] 0 [4, 7, 8, 8, 7, 4, 9] 1 [4, 7, 8, 8, 7, 4, 9] 2 [4, 7, 8, 8, 7, 4, 9] 1 [4, 7, 7, 4, 9] 0 [4, 4, 9] [9] ================================ </code></pre>
 

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