-1

Given a list of numbers.

lst=[1,1,2,4,8,2]

find the unique minimum value

Ideally the answer should be 4, But how do we get to 4?

2

4 Answers 4

1

This is one approach.

Ex:

lst=[1,1,2,4,8,2]
print(min([i for i in set(lst) if lst.count(i) == 1]))
# --> 4
Sign up to request clarification or add additional context in comments.

4 Comments

Can you please explain what you did? I can see a for loop, you changed the list to a set removing the duplicates
I am iterating each element of lst and filtering out the elements that have a count not equal to 1 and then applying min.
This works but is highly inefficient for large collections, leaving it here as a note.
Here are some guidelines for How do I write a good answer?. This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. From review.
0

Basically, how you do this is you first sort the list. And then you check from the lowest if the count == 1. If the count != 1 keep checking until you find the 'unique' number.

def get_lowest_and_unique(lst):
    for x in sorted(list(set(lst))):
        if lst.count(x) == 1:
            return x
    print("Could not find the correct element!")

Comments

0

I would say that the best option is to use collections.Counter:

from collections import Counter
lst=[1,1,2,4,8,2]
c = Counter(lst)
min(v for v, n in c.items() if n == 1)
4

Comments

-1

try

t = list(set(lst))
list.sort(t)
unique_min = t[0]

5 Comments

This evaluates to 1 while the answer should be 4.
It's also the same, as regular min(lst)
@GrzegorzSkibinski It's the same as min(sorted(lst))
doing set(lst) makes all elements unique... You need to first filter-out the duplicates and then look for the minimum. @Philipp well, min(sorted(lst)) and min(lst) should probably be the same thing...
@Tomerikoo That is correct. I was thinking that min would be just accessing the first element for some reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.