I was searched this problem in many websites not get this problem..but i see this problem in ur channel to understand very easily and learn clearly... Thank u brother 🙏
wow i watched so many video but no buddy explained to butter and not cover to all every concept but u clear all the concept all and i"m surprise i means wow ; I"m say to thank u to u " sat sat naman app ko Hindi me bole to "
I love how made the solution looked very simple. I wish you can treat more basic Java Algorithms so that i can understand several approaches to solving algorithms.
The last line causes ambiguous behaviour if there is no element in the array. i.e. the size of the array is zero. Also using constant space method the final array will be 1,2,3,4,3,3,4,4 as we are using the same array and we don't clear the memory for those.
One doubt here. While using same array to avoid duplicates j ended up with 3 here , then what happened to elements present in index 4,5,6,7 of array arr as we didn’t remove them in that array
There is an error in his explanation at least on the O(1). It should be a difference between the array his looping through AND a new array. He treating All as the same array.
Great explanation sir ,but one small problem ,if the last element in the original array is duplicate to second last element in array ,then we have to check if arr[j] != original[n-1]
No you are wrong if last 2 elements are duplicate it will not consider the 2nd last element and it will get out of the loop and finally insert the last element in new array
Hi Vivek, What is the total time complexity for both the solutions. I believe that initially sorting the array will also take some time. So in this case can you please explain?
sir...in constant space method, u churn out output to be [ ]{ 1, 2 ,3 ,4}... but according to your expounded algorithm, output comes out to be [ ]{1,2,3,4, 3,3,4,4 } as deletion of redundant 3,3,4,4 never undergoes....plz explain???????????
In the first approach zeros will be added at the end of the temp array as we are creating with the same size of the actual array. and int he second approach you didnt explain how to remove the last elements. Can you please add the code for the above in your git
// use j++ to store last element //c programe to remove the duplicates elements #include void remove_duplicate(int arr[],int n) //n=number of elements in an array { int j=0; //traversing element in an array for(int i=0; i
In this case we can not go to the last index. Because we can not compare it to the next index after it, that is a not existing index. We have to break out from the loop before we get to the last index. But we can handle the problem with transferring the last index's element to the new array. the basic method: the length of the array is represented by n. in this case n = 9, it means we have 9 elements. this helps us beacuse as you see indexes in the array are starting from 0 to n - 1. In our case, 0, 1, 2, 3, 4, 5, 6, 7, 8, note that 8 = 9 - 1 (n - 1). and this means that if we are the 8th iteration, we will go inside the if conditions body, but after the 8th (again, n - 1) we break from the loop.
#include void main(){ int n=8; int a[8]={1,2,2,3,3,3,4,4}; int j=0; for(int i=0 ; i < n-1 ; i++){ if(a[i]!=a[i+1]){ a[j]=a[i]; j++; } } a[j]=a[n-1]; for(int i=0 ; i < j+1 ; i++){ printf("%d" , a[i]); } } i think this works fine while printing we have to go till j+1 and not n when we do so we get the correct output but is there anyother method .
Hi Vivek, Can you do the same using hash tables. I am interested in known how to effectively create the hash function so that we dont have a large hashvalue. also how to avoid clash. Ex 0%20 is 0 and 20%20 is 0so using modulo is not that effective way of creating hash.
but if we use same array after filling the elements into array ,still will get duplicates after the loop in above example after filling the elements the index j is point to 3 and elements are filtered ,but if we display the array will get all elements after index 3 also,is this expected behavior
It will work. Nothing will be added during the for loop since the elements are same. But we add the last element outside the for loop which is 2 in your case. Hope you understood. If not, try using a pen and paper for each i.
The code snippet to get the Length of the array without considering the duplicates is below: public class Abc { public static void main(String[] args) { int[] a = {1, 2, 2, 3, 4, 4, 4, 5, 5}; int n = a.length; lengthOfArrayWithoutDuplicates(a); } private static int lengthOfArrayWithoutDuplicates(int[] a) { int count = 1 ; for (int i = 1; i < a.length; i++) { if(a[i-1] != a[i] ) { a[count++] = a[i]; } }//for System.out.println("length of array without duplicates " +count); return count; } }
Thank you. But one doubt In using extra space, what if the last element is 4 ( instead of 5 in the original array). Then the temp array would end up having 4 twice - pls correct me if I am wrong. Thank you for your help.
I was searched this problem in many websites not get this problem..but i see this problem in ur channel to understand very easily and learn clearly... Thank u brother 🙏
jai bolo chatgbt 😅
Thank you!!! very well explained!! wish we had a professor like you!!!
wow i watched so many video but no buddy explained to butter and not cover to all every concept but u clear all the concept all
and i"m surprise i means wow ;
I"m say to thank u to u
" sat sat naman app ko Hindi me bole to "
sir take all interview probelms from geeksforgeeks and dicuss their algorithm and complexity
Plz help me bro...
I have to prepare for placements plz tell where should I refer
@@sahad_abd bro,i am not preparing for interview now😅
@@Gaurav-bv6xr plz suggest me brooo..just you know
@@sahad_abd prepare interview ques from geeksforgeeks
I love how made the solution looked very simple. I wish you can treat more basic Java Algorithms so that i can understand several approaches to solving algorithms.
Your explanation technique is great 👍
awesome explanation sir, hats off, keep uploading, thank you 👍👍👍👍🙂🙂🙂🙂
The last line causes ambiguous behaviour if there is no element in the array. i.e. the size of the array is zero.
Also using constant space method the final array will be 1,2,3,4,3,3,4,4 as we are using the same array and we don't clear the memory for those.
it's for sorted array
You didn't get his point. He's right, you will need to remove the remaining elements from the array after Jth position
Exactly.. I have tried.. the last elements remain in array..
That's right! We need to run a loop from jth+ 1 index till the last (n-1) and clear the remaining elements of the array.
@@tonmoyrakshit1717 Can u give the code to clea remaining elements
Nice lecture sir keeping uploading this type of conceptual video
🥰🥰🥰🥰🙏🙏🙏🙏🙏🙏 Maja aa gaya 🙂
Very well explained, even small details are touched that others missed
Your way of teaching is awesome.
we need more problems solution on array, so update with some more problems.
Thank you Sir for helping . I've been searching a lot for the logical explanation.
Nice explanation sir .. well done .. Thank u so much for crystal clear explanation👍
Very Good! Please discuss more complicated problems from GeeksforGeeks.
good explanation sir
One doubt here. While using same array to avoid duplicates j ended up with 3 here , then what happened to elements present in index 4,5,6,7 of array arr as we didn’t remove them in that array
print array from 0 to j
@@gokulnathnallaiya7617 thanks buddy
Very simple explanation sir
Thank you bro for your visualization of the problem and the solution is awesome. finally, I got it 🙏🏿
Very much helpful and can understand by anyone as explanation is very simple . Expecting to see the videos for java collections problems.
Liked the way you explained! Really Helpful.
what a quality explanation!. big thanks for the effort
thank you sir you explained it so easily
make these kinds of tricky questions which provide best solution for a particular problem
Very good explanation
Thanks for uploading it.. You are doing a great great job.. Please please upload more of such programming questions.. We really need it
sir really good and help me a lot
You may want to slice of all the elements after j index, since you are doing in place replace.
correct, Even i have commented the same
thank you so mush you are brilliant and you have such a beautiful method of teaching
Thank you sir 🙏 everything is clear now
Great explanation
I feel sorry to say, but I wasn't able to get the result as you explained on the video(even though I have written the code as yours).
There is an error in his explanation at least on the O(1). It should be a difference between the array his looping through AND a new array. He treating All as the same array.
You need to start the loop from I=0;I
☠️☠️🤷🏿♂️🤷🏿♂️🤧🤧sorry to hear that
awesome tutorial sir
Very good lesson !!
you are completely ossum sir!!
Very Well Explained .Thanks a lot
Great explanation sir ,but one small problem ,if the last element in the original array is duplicate to second last element in array ,then we have to check if arr[j] != original[n-1]
Agreed !!
No you are wrong if last 2 elements are duplicate it will not consider the 2nd last element and it will get out of the loop and finally insert the last element in new array
@@sdani9160 yes correct it will not take any of the duplicates from ending array
We kept i
i
Hi Vivek,
What is the total time complexity for both the solutions. I believe that initially sorting the array will also take some time. So in this case can you please explain?
you are the loveliest indian ever.
Thanks sir .. really helpful
Thanks a lot
Awesome Explanation thank you sir
Thank you well explained
Thanks a lot, Sir!! Simplified explanation..:)
sir...in constant space method, u churn out output to be [ ]{ 1, 2 ,3 ,4}...
but according to your expounded algorithm, output comes out to be [ ]{1,2,3,4, 3,3,4,4 } as deletion of redundant 3,3,4,4 never undergoes....plz explain???????????
same doubt
Since you know j=3, which means first 4 are distinct elements.
So you can use your unique values in the array by
for(int i = 0; i
its really very helpful..Thanks
Very helpful. Thank you very much.
Thank you so much sir❤
In the first approach zeros will be added at the end of the temp array as we are creating with the same size of the actual array. and int he second approach you didnt explain how to remove the last elements. Can you please add the code for the above in your git
Thanks for the video. Something is missing at the end for the "in memory" case , test [1,2,3,3]
you need to add at the end
while j
thanks brother amazing...
Very clear and understandable! thanks!
Sir nice explanation
very clear and good writing on whiteboard
Great explanation. Thanks!
// use j++ to store last element
//c programe to remove the duplicates elements
#include
void remove_duplicate(int arr[],int n) //n=number of elements in an array
{
int j=0;
//traversing element in an array
for(int i=0; i
thank you sir..please post more..we will be very thakfull sir
Thank you, I have a question sir what is the n-1 stands for inside the for loop? Thank you
In this case we can not go to the last index. Because we can not compare it to the next index after it, that is a not existing index. We have to break out from the loop before we get to the last index.
But we can handle the problem with transferring the last index's element to the new array.
the basic method:
the length of the array is represented by n. in this case n = 9, it means we have 9 elements.
this helps us beacuse as you see indexes in the array are starting from 0 to n - 1. In our case, 0, 1, 2, 3, 4, 5, 6, 7, 8, note that 8 = 9 - 1 (n - 1).
and this means that if we are the 8th iteration, we will go inside the if conditions body, but after the 8th (again, n - 1) we break from the loop.
Second last elements
Good explanation buddy, a just one quick question to you. What if the last value is 4 instead of 5 then?
there is something wrong with this algo.... use arr[j++]=arr[i] and arr[j++]=arr[n-1]
@@dev_manish, the algo is wrong!
Good work.really helpful
Thank you very much sir...
great explanation!
Hi Vivek,
Here one more step is needed to remove other elements from array from Jth position,
#include
void main(){
int n=8;
int a[8]={1,2,2,3,3,3,4,4};
int j=0;
for(int i=0 ; i < n-1 ; i++){
if(a[i]!=a[i+1]){
a[j]=a[i];
j++;
}
}
a[j]=a[n-1];
for(int i=0 ; i < j+1 ; i++){
printf("%d" , a[i]);
}
}
i think this works fine
while printing we have to go till j+1 and not n
when we do so we get the correct output
but is there anyother method .
shout out to you bro, I hope someone posted you on national boyfriend day
in first case the last element is 4
o/p becomes----1 2 3 4 4??
nicely explained.thanks
Very nice Sir thank you
if the arr is 1 2 3 1 2 3 then its not able to find the duplicate that you discussed in the every last moment.
if we keep modifying the same array will the size of the array change?
sir If the duplicates are not in consecutive then what we have to do?
what about 1,2,2,3,3,1,4,4.... can we apply this algo in situation?
Thank you so much sir
What if the duplicate of 2 is place on the last place on array list
Janis Paloma - hi, the question n solution is on sorted array.
Super class....
the Solution does not work for constant space arr= [1,1,2] as J value remains 1 and hence when we want to print the array form 0 to j it will fail.
Hi Vivek, Can you do the same using hash tables. I am interested in known how to effectively create the hash function so that we dont have a large hashvalue. also how to avoid clash. Ex 0%20 is 0 and 20%20 is 0so using modulo is not that effective way of creating hash.
Great Sir
//Remove duplicates from Array
let arr = [1,2,3,2,4,3,5];
let res = getResult(arr);
function getResult(arr){
let obj = {};
let res = [];
for(let i=0;i
Hi I really appreciated your video. But one question, what does n - 1 mean?
Zohaib Shahzad n is total number of array elements
if a[10] then n is 11 cause index of array starts from 0
-1 the length of the array
Can someone show how can we print the temp[j]...
Nice explain sir
Hi Vivek, pls explain, how to remove the eliments of linked list which is having sum is equals to zero
i++ j++ will not execute if duplicate found. Sort is required to place duplicate side by side.
correct in place solution:
def remove_duplicates(nums):
j = 0
for i in range(len(nums) - 1):
if nums[i] != nums[i + 1]:
nums[j + 1] = nums[i + 1]
j += 1
return j + 1
great video keep it up buddy
Can anybody explain what is the time complexity of this algorithm?
Why can't we use just list(set(arr))???
Sir output kuda print cheyyochu kadha sir
Vivekanand sir
can i get java pgm for this ??
Sir also tell complexity in each case .
If last element and before element are equal what is the code sir
good quality education.
but if we use same array after filling the elements into array ,still will get duplicates after the loop
in above example after filling the elements the index j is point to 3 and elements are filtered ,but if we display the array will get all elements after index 3 also,is this expected behavior
Sir but if all the elements are same this code will not work
For example if array elements are:2 2 2 2 2
It will work.
Nothing will be added during the for loop since the elements are same. But we add the last element outside the for loop which is 2 in your case.
Hope you understood. If not, try using a pen and paper for each i.
what about the remaining elements in the original array after index 4?
you are right , he needs to go every after the last j, and in every place to put '/0' , or null .
depend of the type of the array.
print upto j.. no need to print after that
Sir, please tell how will u print the output as value of n decreases after removing duplicate
You have value of j.. you can use it to iterate throught new array.and print the elements
But that will be the contion j< upto what? To print the element
The code snippet to get the Length of the array without considering the duplicates is below:
public class Abc {
public static void main(String[] args) {
int[] a = {1, 2, 2, 3, 4, 4, 4, 5, 5};
int n = a.length;
lengthOfArrayWithoutDuplicates(a);
}
private static int lengthOfArrayWithoutDuplicates(int[] a) {
int count = 1 ;
for (int i = 1; i < a.length; i++) {
if(a[i-1] != a[i] ) {
a[count++] = a[i];
}
}//for
System.out.println("length of array without duplicates " +count);
return count;
}
}
Thank you. But one doubt In using extra space, what if the last element is 4 ( instead of 5 in the original array). Then the temp array would end up having 4 twice - pls correct me if I am wrong. Thank you for your help.
replace i
nice video thanks
Could you a make video on Chess Design
Can i see the whole program of this please?