single element in a sorted array leetcode | single element in a sorted array leetcode python

preview_player
Показать описание
Please like the video, this really motivates us to make more such videos and helps us to grow. thecodingworld is a community which is formed to help fellow student and professionals looking to crack the “coding interviews”.

We love solving and sharing problems which are frequently asked in technical interviews and voted by community at various websites.

✅ For more and upcoming updates join our community

#leetcode #python
Рекомендации по теме
Комментарии
Автор

Really appreciate it brother. Idk why but i was unable to understand any other solution that were using the odd-even index concept. Just wasnt able to understand it and your approach really clicked instantly. Thanks a lot!

dhruvmaindola
Автор

bro I used dictionary and it became an easy one for me
great job btw.


dic = {}
for i in range (len(nums)) :
if nums[i] in dic :
dic[nums[i]] = dic.get(nums[i]) + 1
else :
dic[nums[i]] = 1
for key in dic.keys() :
if dic.get(key) == 1 :
return key

return -1

saugatgarwa
Автор

There is a closed-form mathematical solution : def singleNonDuplicate(self, nums: List[int]) -> int:
actualSum = sum(nums)
projectedSum = sum(list(set(nums))) *2
return projectedSum-actualSum

kkveena
Автор

I really appreciate the time and effort you put into explanation and visualization. Looking forward for more solutions for medium problems.

tolegenazamat
Автор

Here is my code.
Can you please tell me the run time complexity and if its good?
Hashtable is my goto method for such type of questions.
Please let me know your input.

class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:

if not nums:
return

for ele in count:
if count[ele]==1:
return ele

shlokjain
Автор

Can you please what is the reason behind this logic like how you came up with this

code_school
Автор

class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
return sum(set(nums))*2-sum(nums)
Optimised Solution

viratsharma
join shbcf.ru