3
\$\begingroup\$

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.

\$\endgroup\$
18
  • 1
    \$\begingroup\$ You must specify the winning criterion clearly in your post. \$\endgroup\$ Commented Jul 15, 2017 at 17:53
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 15, 2017 at 18:00
  • 2
    \$\begingroup\$ @SatishPatel You should specify that in the challenge. \$\endgroup\$ Commented Jul 15, 2017 at 18:02
  • 3
    \$\begingroup\$ Also, I suggest making it code-golf. \$\endgroup\$ Commented Jul 15, 2017 at 18:02
  • 5
    \$\begingroup\$ 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 \$\endgroup\$ Commented Jul 15, 2017 at 18:48

1 Answer 1

1
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ I got values between 1.1126001481898129e-05 and 8.827599958749488e-05 for the test cases in the challenge. \$\endgroup\$ Commented Jul 15, 2017 at 18:24
  • \$\begingroup\$ @Mr.Xcoder Okay, thanks. This is using bash's built in time which may be off, I don't know for sure. \$\endgroup\$ Commented Jul 15, 2017 at 18:39
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 15, 2017 at 18:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.