-1

I am trying to create a function in which it removes duplicates from a list, but it fails whenever there is a 1 in the list. It works for all the other numbers and I am not sure what is causing this. There is an array with some numbers. It’s guaranteed that array contains more than 3 numbers.

def find_uniq(arr):
    new_list = []
    for i in arr:
        if i not in new_list:
            new_list.append(i)
    # this returns the second value in new_list as there are two values in the list.
    return new_list[1]
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Why are you returning `new_list[1]` in the end? – blhsing Feb 23 '19 at 00:46
  • new_list originally returns two values [a,b]. the number i am looking for is b, so i am only showing the second value. – Krish Patel Feb 23 '19 at 00:47
  • Why would `new_list` only have two values? It will have as many values as there are unique elements in the input. – Barmar Feb 23 '19 at 00:48
  • Please post complete version of your code. Besides, any reason not to use `list(set(arr))`? – Chris Feb 23 '19 at 00:49
  • If the input only has one element, how could there be two values in `new_list`? If you want to return the last unique element, use `new_list[-1]`. – Barmar Feb 23 '19 at 00:49
  • When I ran the function without the [1], new_list had two values. The first value was of no use. I had to add the [1] to the function as that was the only number that wasn't a duplicate. – Krish Patel Feb 23 '19 at 00:50
  • this is all the code i have – Krish Patel Feb 23 '19 at 00:50
  • Why is the first value of no use? Don't you want all the unique values? – Barmar Feb 23 '19 at 00:50
  • This was an exercise on this website. https://www.codewars.com/kata/585d7d5adb20cf33cb000235/train/python – Krish Patel Feb 23 '19 at 00:51
  • There is only 1 unique value according to the exercise. I don't know why it was returning two, but there should only be 1. – Krish Patel Feb 23 '19 at 00:51
  • The question says "It's guaranteed that the array contains more than 3 numbers" and "All numbers are equal except for one." So the problem you describe should never occur. – Barmar Feb 23 '19 at 00:53
  • But there's no reason to assume that the number you want will always be the second number. If the input is `[1, 2, 2, 2, 2, 2]` the unique element is `1`, but the second element of `new_list` will be `2`. – Barmar Feb 23 '19 at 00:54
  • Copy the description of the problem from codewars into the question. Your explanation is not clear enough. – Barmar Feb 23 '19 at 00:55
  • You can return as soon as `new_list` has at least 2 elements and you detect a duplicate. Whichever element doesn't match the current list element is the unique one. – Barmar Feb 23 '19 at 00:57
  • From the two values in new_list, how could I detect which is a duplicate? – Krish Patel Feb 23 '19 at 00:57
  • Read my last comment. – Barmar Feb 23 '19 at 00:58
  • If that condition never occurs, it means the unique element is the last element of the list. – Barmar Feb 23 '19 at 00:59
  • What would the code look like? Is there an if new_list[0].appear command? – Krish Patel Feb 23 '19 at 01:00
  • Possible duplicate of [How do you remove duplicates from a list whilst preserving order?](https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-whilst-preserving-order). – martineau Feb 23 '19 at 01:28

1 Answers1

0

You don't need to make a new list containing all the unique values in the list. As soon as you detect a duplicate, you just need to return the other number, since it's guaranteed to be unique.

The problem states that the input is guaranteed to contain at least 3 elements. You can start by checking the first 3 elements, to see if any of them is unique:

if input[0] != input[1]:
    if input[0] == input[2]:
        return input[1]
    else:
        return input[0]
elif input[1] != input[2]
    if input[0] == input[1]:
        return input[2]
    else:
        return input[0]
elif input[0] != input[2]
    if input[0] == input[1]:
        return input[2]
    else:
        return input[0]

If you get past this, it means the first 3 elements are duplicates. You can simply loop through the rest of the input, looking for the first element that's not equal to this.

dup = input[0]
for el in input[3:]:
    if el != dup:
        return el
Barmar
  • 741,623
  • 53
  • 500
  • 612