I am trying to find out lowest unique element from list. I have been able to generate O(n^2) and O(n) solutions. But I don't find them optimized. Kindly help me understand,if there is a possible O(n) solution for it. No one liner solutions please. Below are my codes:
Main function:
if __name__ =="__main__":
print uniqueMinimum([6, 2, 6, -6, 45, -6, 6])
print lowestUnique([5, 10, 6, -6, 3, -6, 16])
O(n^2) Solution:
def lowestUnique(arr):
num = max(arr)
for i in range(len(arr)):
check = False
for j in range(len(arr)):
if arr[i]==arr[j] and i!=j:
check =True
if check==False:
if num > arr[i]:
num = arr[i]
return num
I would like to avoid using max(array) in above solution.
O(n) Solution:
def uniqueMinimum(array):
d ={}
a =[]
num = max(array)
for i in range(len(array)):
k =d.get(array[i])
if k is None:
d[array[i]] = 1
a.append(array[i])
else:
d[array[i]] = k+1
if array[i] in a:
a.remove(array[i])
a.sort()
return a[0]