remove duplicates from array [closed]

You have given an array from which we need to remove duplicates.

Rules:

1. First or immediate duplicates gets removed first.

2. Array contains natural numbers a[i]>0 for i>=0

3. no. of elements in the array is greater than 1 i.e. n>1

e.g.

{1,2,3,4,5,5,4,6,6,7,8}=> {1,2,3,7,8}
{1,2,3,3,4,5,6,7,7,8} => {1,2,4,5,6,8}
{1,1,3,3,4,4,6,6}=>{}
{4,2,6,8,3,1,1,3,8,6,2,4}=>{}
{6,6,6,1}=>{6,1}


Only adjacent duplicate elements like [1, 2, 2, 1] are removed, but not [1, 2, 1, 2]. This process is repeated until no further modifications are made.

For winning criteria is the one which gives the fastest result.

• You must specify the winning criterion clearly in your post. Jul 15, 2017 at 17:53
• Why {6,1}? 6 is a duplicate. Then, {1,2,3,4,5,5,4,6,6,7,8} should output {1,2,3,4,5,6,7,8} as well. Jul 15, 2017 at 18:00
• @SatishPatel You should specify that in the challenge. Jul 15, 2017 at 18:02
• Also, I suggest making it code-golf. Jul 15, 2017 at 18:02
• If you want to keep this as fastest ode (which is fine) you'll need to provide larger test cases as yours would complete almost instantaneous in any sane language and be subject to significant noise in times Jul 15, 2017 at 18:48

Python 2, 201 bytes

array = input()
index = 0
while index < len(array) - 1:
if index >= 0 and array[index] == array[index + 1]:
array = array[:index] + array[index + 2:]
index -= 1
continue
index += 1
print(array)


Try it online!

Optimizations thanks to Mr. Xcoder

• I got values between 1.1126001481898129e-05 and 8.827599958749488e-05 for the test cases in the challenge. Jul 15, 2017 at 18:24
• @Mr.Xcoder Okay, thanks. This is using bash's built in time which may be off, I don't know for sure. Jul 15, 2017 at 18:39
• I get slightly better times if I add continue at the end of your if statement and drop the else (between 1.0405001376057044e-05 and 4.7165998694254085e-05), because it skips the evaluation of the else-statement. Jul 15, 2017 at 18:46
• @Mr.Xcoder Interesting, thanks. Jul 15, 2017 at 18:55