the if else block can be simplified. public class Solution { public int SingleNonDuplicate(int[] nums) { /* #1. elements are in pair, then in ideal case it will start from even index & end at odd index #2. for single element, element != nums[prev] && element != nums[next] */ int n = nums.Length; if (n == 1) return nums[0]; if (nums[0] != nums[1]) return nums[0]; if (nums[^1] != nums[^2]) return nums[^1]; int l = 2; int r = n - 2; while (l
Similar solution but using indexes : Java Solution class Solution { public int singleNonDuplicate(int[] nums) { int n = nums.length; int s = 0; int e = n-1;
My solution is also the same but there is little difference. can you please check and tell me if it does make a difference in the performance of the code? class Solution { public int singleNonDuplicate(int[] nums) { int start = 0; int end = nums.length-1; while(start < end){ int mid = start + (end-start)/2; //Check number of elements on the right side boolean isEven = (end-mid)%2 == 0 ? true : false; if(nums[mid] == nums[mid+1]){ if(isEven){ start = mid+2; } else{ end = mid-1; } } else{ if(isEven){ if(nums[mid-1] == nums[mid]){ end = mid-2; } else{ return nums[mid]; } } else{ start = mid+1; } } } return nums[start]; } }
Subhanallah 😊😊 kya shaandar samjhaya aapne
most underrated channel for dsa keep up doing sir
nice explanation with diagrams. 😄
Thanks a lot for making this look too easy
thanks sir for providig us this types of explanantion
after watching this......problem become piece of cake, THANKYOU
great work , keep up doing sir.
this is also a problem on bit manipulation taking xor sum, if the array wouldn't have been sorted
why ?
Class solution {
Public int single Non Duplicate (int[]nums){
int s=0 e=nums.length;
While (s
the if else block can be simplified.
public class Solution
{
public int SingleNonDuplicate(int[] nums)
{
/*
#1. elements are in pair, then in ideal case
it will start from even index & end at odd index
#2. for single element, element != nums[prev] && element != nums[next]
*/
int n = nums.Length;
if (n == 1)
return nums[0];
if (nums[0] != nums[1])
return nums[0];
if (nums[^1] != nums[^2])
return nums[^1];
int l = 2;
int r = n - 2;
while (l
Similar solution but using indexes : Java Solution
class Solution {
public int singleNonDuplicate(int[] nums) {
int n = nums.length;
int s = 0;
int e = n-1;
while(s < e){
int mid = s + (e-s)/2;
boolean odd = false;
boolean even = false;
if((mid % 2) == 1){
odd = true;
}
if( (mid % 2) == 0){
even = true;
}
boolean isNextEqual = false;
if(nums[mid] == nums[mid+1]){
isNextEqual = true;
}
if( (odd && isNextEqual) || (even && ! isNextEqual) ){
e = mid;
}
else if( (odd && ! isNextEqual) || (even && isNextEqual) ){
s = mid+1;
}
}
return nums[s];
}
}
understood
Class solution {
Public int singlesNon Duplicate (int[]nums){
int s=0 e=nums.length;
While (s
class Solution {
public:
int singleNonDuplicate(vector& nums) {
int n = nums.size();
int ans = 0;
for(int i = 0;i
Thank you soo much
Thank you for watching ❤️❤️❤️
2nd approach correction s=mid+2
❤️❤️❤️
I am watching this solution after attempting Amazon SDE Online Assessment
Can you guide me for prepation for coding test
Hii can we do this question with map as well?
time complexity by using map will become O(n logn) , which will be bigger than O(logn)
My solution is also the same but there is little difference. can you please check and tell me if it does make a difference in the performance of the code?
class Solution {
public int singleNonDuplicate(int[] nums) {
int start = 0;
int end = nums.length-1;
while(start < end){
int mid = start + (end-start)/2;
//Check number of elements on the right side
boolean isEven = (end-mid)%2 == 0 ? true : false;
if(nums[mid] == nums[mid+1]){
if(isEven){
start = mid+2;
}
else{
end = mid-1;
}
}
else{
if(isEven){
if(nums[mid-1] == nums[mid]){
end = mid-2;
}
else{
return nums[mid];
}
}
else{
start = mid+1;
}
}
}
return nums[start];
}
}