remove duplicates from array

3

0

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.

Satish Patel

Posted 2017-07-15T17:51:35.440

Reputation: 131

Question was closed 2017-07-15T18:57:54.993

1You must specify the winning criterion clearly in your post. – Mr. Xcoder – 2017-07-15T17:53:56.397

1Should {6,6,6,1} output {1} or {1,6}? Add this on the test-case, maybe? – officialaimm – 2017-07-15T17:58:14.270

1@officialaimm, It should output {6,1}. – Satish Patel – 2017-07-15T17:59:04.890

2Why {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. – Mr. Xcoder – 2017-07-15T18:00:10.233

@Mr.Xcoder, first matching duplicates gets removed. so next 6 is not having any immediate matching element so first 6's will be removed and 6,1 is remaining in the result. hope this helped – Satish Patel – 2017-07-15T18:01:50.907

2@SatishPatel You should specify that in the challenge. – Mr. Xcoder – 2017-07-15T18:02:34.863

3Also, I suggest making it [tag:code-golf]. – Mr. Xcoder – 2017-07-15T18:02:55.573

1Code golf or fastest code please, not both. And if it is a code golf, please say it in your question, not with the tags. – caird coinheringaahing – 2017-07-15T18:10:31.017

@cairdcoinheringaahing, thanks for the suggestion. edited as required. – Satish Patel – 2017-07-15T18:12:04.257

1fastest-code and fastest-algorithm don't go together. – dzaima – 2017-07-15T18:35:20.783

Does the returned array need to be sorted? – powelles – 2017-07-15T18:41:40.870

@powelles {6,1} is not sorted... – HyperNeutrino – 2017-07-15T18:42:53.780

@HyperNeutrino But it still follows the order of the original array. I guess I mean does the sort order matter at all. – powelles – 2017-07-15T18:45:10.837

@powelles Ah okay. We'll wait for OP clarification then. I don't think it matters too much since even if it increases byte count that doesn't matter, but there could be some optimizations, so I don't know for sure. – HyperNeutrino – 2017-07-15T18:47:25.177

5If 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 – Downgoat – 2017-07-15T18:48:06.763

Often problems with (simple) polynomial-time algorithm is not suitable for fastest-code. – user202729 – 2017-07-16T03:41:55.350

1

Next time, please consider using the Sandbox.

– musicman523 – 2017-07-16T04:01:44.290

@musicman523, thanks for your feedback. I will make sure next time post question on sandbox first then on the main page. thanks again :) – Satish Patel – 2017-07-16T04:33:55.243

Answers

1

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

HyperNeutrino

Posted 2017-07-15T17:51:35.440

Reputation: 26 575

I got values between 1.1126001481898129e-05 and 8.827599958749488e-05 for the test cases in the challenge. – Mr. Xcoder – 2017-07-15T18:24:59.770

@Mr.Xcoder Okay, thanks. This is using bash's built in time which may be off, I don't know for sure. – HyperNeutrino – 2017-07-15T18:39:31.647

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. – Mr. Xcoder – 2017-07-15T18:46:39.197

@Mr.Xcoder Interesting, thanks. – HyperNeutrino – 2017-07-15T18:55:49.733