Quick note: Instead of declaring the length outside the for loop you can declare and initialize it at the beginning of the loop like this: for(let i=0,length=a.length;i
5 лет назад+21
Believe it or not, for (let i = 0; i < a.length; i++) is actually faster. Test it yourself using jsperf. There is no need to store the length of an array in a separate variable, the JS engine does it for you automatically. On Firefox, I get about 0.69% decrease in performance when I store the length in a a separate variable. Insignificant, but illustrates that there is no point in doing it.
DUDE you are a magician. I was stuck on TheOdinProject exercise 4, removeFromArray. I spent 3 days reading up on documentation understanding rest parameters, spread syntax, numerous array methods, making sure I was looping correctly and just was not understanding in the slightest why my solution wasn't working. Thanks for helping me understand better, i never would have thought of using indexOf with a strict equality to -1. That's a pretty neat trick. I'm so excited to learn more! Edit: Just checked their problem solution... wow i feel dumb 🤦
cool video... I noticed two things... First sort: if in the array we have numbers that have more than one digit, in js, sort will be messed up. sort fn should be: sort(a, b) {return a < b} Second when you want to use obj to dedup, the deduped array will be array of strings not array of numbers.
That's what I thought initially, but I think it'll work anyway because the sort will group the duplicates together, even if they're not overall in the right order. I believe the algorithm will still remove the duplicates.
Man I would like to thank you for this channel. I'm really enjoy javascript but I don't feel confident enough to go to a job interview ... I know fpr sure that this and you other channel will boost my confidence thnx you in advance sir
Way back I had watch this video to learn to remove duplicate items and now in the interview I got this same question. Initially I show him by using indexOf method and again he was not satisfied with this method saying I will have to push the item to the array everytime so this will consume more time. So I again show him the object property method like you have explain in this video and return the Object.keys(obj) . he was very happy. More couple of question and now I got selected in technical round. Thank you @techshit @interviewnest Somehow this channel was not subscribed. I was so unlucky. I subscribed it now.
Using the Object.keys(obj) way returns string values and you begin with numbers, so shouldn't the interviewer want the values back in the same data type ?
I had the same remark while watching the video 😁 and I think that it's not going to be faster because you'll need to do another loop to convert them into numbers
Actually, using let len = a.length; for (let i = 0; i < len; i++) { } is **slower** than simply writing for (let i = 0; i < a.length; i++) { } Test it yourself using jsperf. The runtime engine already allocates a variable for length, so doing the same thing manually is only adding to the performance cost.
I love your videos, thank you for your time and effort. Removing a duplicate element from an array is a tricky thing.By using collection(in Javascript it is Set) it can be done easily.
Hey, I love your tutorials and the way you explain everything line by line. Liked, Subscribed and already shared your tutorials with my colleagues. Looking forward for your upcoming videos. Just noticed, Object.keys method provided all the string values in the array.
Good catch. The only way I see of fixing that is to loop through "b[ ]" and parseInt( ) each member of the array. It's a shame that you can't include type conversion as an argument in the Object.keys( ) method. In which case, you're better off just looping through the object and pushing each key to a new array while using parseInt, rather than using Object.keys and looping through it again to change the strings to integers.
@@ajasen You can still use the object pattern, it's just a bit messier. At the least, you need an extra conditional or two. And the more edge cases, the messier the code gets. This isn't to say that you would actually want to use this approach, but it can be done for what you're asking. I'll put the code for the cases you provided below. I added a couple of extra cases for "false" or "undefined", vs false or undefined (string value vs boolean or undefined data type in these cases). P.S. For a Function or Object or BigInt, etc... type as a member of the original array, I would need more logic in my convertToType( ) function, but you get the idea here...
const a = [1, 2, "52634", 1.2, "false" ,"1", "dog", false, false, 4.5, 5.5, 1.2, 5, "cat", "cat", true, null, undefined, undefined, 2, true, null, "null", false, null, 1, 8]; function eliminateDups(theArray) { let obj = {}; // I threw in a statement for distinguishing edge cases of strings that could be defined as other types such as "undefined" the string vs undefined the data type or "4" vs 4. for (const key of theArray) { if(typeof key === "string"){ obj["%String%"+key] = true; } else { obj[key] = false; } } let b = [ ]; //assignment of the temp tests for a string then for a number then converts otherwise; for( key in obj ){ let temp = (obj[key] === true ? key.slice(8) : ( parseFloat(key) || convertToType(key) )); b.push( temp ); } console.log("b: ") console.log(b); return b; } function convertToType (theKey) { switch(theKey){ case "null": return null; break; case "undefined": return undefined; break; case "false": case "true": return theKey === "true"; break; default: return theKey; } } console.log(eliminateDups(a));
Another one liner could be let arr = [ 1, 2, 5, 2, 1, 8 ]; arr.filter( (num, index) => !ar.slice(0, index).includes(num) ); Maybe not performatic chaining filter, slice and includes, but at least it is pretty (: Also it is worth noticing that the third solution (the one that converts array to object and then get the keys) will make the numbers get converted to string. in the final array In other words, we are getting [ "1", "2", "5", "8" ] as the result. We could use the numbers as values instead of "true" and use Object.values(obj) to get them back in array form: let arr = [ 1, 2, 5, 2, 1, 8 ]; let obj = {} arr.forEach( num => obj[num] = num ) Object.values(obj) // this returns [ 1, 2, 5, 8 ]
Nice video thx for making this video plz make more videos like this. Very informative and easy to understand for the biggners ❤👌plz make more videos on interview questions so that people who do not gave interviews yet and this is the first time going for the interview these questions are very helpful for them ❤❤
is the complexity mean how bad/good the code is ? nd these O nd omegas are like the degree of how bad/good is it ? ps : I have no idea, just wanted a quickflash clue
Hey a small correction, the complexity for quick sort is nlogn and regarding the a length it doesn't calculates the length every time but is stored and whenever you update values js does it automatically.
You explain concepts so well always! and thank you for mentioning the complexity difference. Very informative! and I saw your closures video but it was somewhat confusing. It would be great if you can explain in more depth with different example
Thanks for the wonderful concepts. Everytime I watch your video, I always learn something new. Do you have an elaborate video on sets and it's work around?
On monday i will release a video n maps and weakmap data structures. this would be on my techsithtube channel . I will cover sets and weaksets in next. video.
I was there to comment something like "You now there is a problem with js when you see a 12 minutes video explaining how to remove duplicates elements from an array". But your comment do the job well enough ^^
5 лет назад
Sure, but this is even slower than doing it in Python (and I thought nothing could be slower than Python, before I heard about Ruby). Ruby is utter rubbish.
An alternate way to use an object is like this: const a = [1, 2, 5, 2, 1, 8]; const b = []; const arrValMap = {}; for (const i of a) { if (!arrValMap[i]) { b.push(i); arrValMap[i] = true; } } With this approach, the array values remain numbers rather than being converted to strings.... And I just noticed this video is several years old. Oh well.
6 лет назад
In the first algorithm, you have the variable len defined globally, which is unnecessary (the same reason why we put let i = 0). It's better to write the for loop as this: for(let i = 0, len = a.length; i < len; i++) This way, a.length is accessed only once, which is good, but len is cleared from memory the moment the loop finishes.
Awesome! can you do a video on best practices? In this video you mentioned that create a separate variable for a.lenght instead of using in the for loop. Similar to that, can you make a separate video on best practices in javascript or any other programming languages?
@@danialmalik80 yup, because 'includes' or 'indexOf' is essentially a for-loop that checks for the existence of an item (number in this case). Therefore, there is an outer loop (for-loop) and an inner loop ('includes) -> N^2
Thanks for the answer sir... marking this question as i have seen it ...also really great how you explained the different ways i which we can achieve the answer...and what's best... my immediate approach though was similar to the brute force method...sat down with the problem and thought of other JS specific way i could solve it... been preparing myself for upcoming interviews and your videos are a great help for it ..thank you
damn wish i found you sooner and that i should've studied more. Gonna watch more of your videos and hopefully i'll get better at coding. Thank you so much for these.
Great content as always. Just one thing, Can you specify going forward which complexity are you talking about (time complexity or space complexity) because, it sounded like space complexity when you said 2 arrays, and n square, but we only had 1 loop so the time complexity should be only O(n) and not O(n^2) for brute force
The first example is my favorite because I had already tried to solve this algorithm using a similar method but failed. the problem with my original attempt was that it only looked at the number previous to current selection. So there were still duplicates in the array.
when we are using Object.keys approach, it returns the string values as the elements, hence we again need to convert this to an integer using the array.map, which in turns increases the complexity let a = Object.keys(obj).map((value)=>{ return parseInt(value, 10); })
Any idea why I'm getting any empty array because I'm trying to convert the array elements to integers each using the parseInt method? let a = [8,7,7,8,1,2,3,4,3,5,6,1,2,1,2,3]; let obj = {}; for(let i of a) { obj[ i ] = true; } let b = Object.keys(obj); console.log(Array.from(parseInt(b))); // this returns an empty array `[ ]`
instead of above concept we can use console.log([...new Set(nums)]) will return the unique value. for ascending and descending we can use reverse and sort can use.. your comment required here Sir
For this purpose, it doesn't matter, so long as all identical elements are next to each other. [1,1,1,11,11,2,23,23,3,3,3,33,4] would work because we're only pushing a new item to the resulting array "b", if the previous item wasn't the same as the current one.
The LeetCode specified that "Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory."
Quick note:
Instead of declaring the length outside the for loop you can declare and initialize it at the beginning of the loop like this:
for(let i=0,length=a.length;i
Believe it or not, for (let i = 0; i < a.length; i++) is actually faster.
Test it yourself using jsperf. There is no need to store the length of an array in a separate variable, the JS engine does it for you automatically. On Firefox, I get about 0.69% decrease in performance when I store the length in a a separate variable. Insignificant, but illustrates that there is no point in doing it.
@ Good point if you are working in a browser environment, but I don't know if it's applicable in a nodejs environment tho
I failed this question on an interview and didn’t get the job because of it. Thank you for breaking it down so effectively.
I got the job for the same question. I had watched this video already. and same question came to me.
me too
For me also asked the same question but I just converted array to a set and got the offer 😁
I think we've all been there🥲
Me too 🤣 2 times lost ,
At 9:50 the Value inside are String Types. Object.keys(obj).map(Number) can be helpful. Thanks for your awesome videos and work!
Thanks for the tip!
Great tutorial showing not just the "how" but also the so important "why"!
DUDE you are a magician. I was stuck on TheOdinProject exercise 4, removeFromArray. I spent 3 days reading up on documentation understanding rest parameters, spread syntax, numerous array methods, making sure I was looping correctly and just was not understanding in the slightest why my solution wasn't working.
Thanks for helping me understand better, i never would have thought of using indexOf with a strict equality to -1. That's a pretty neat trick. I'm so excited to learn more!
Edit: Just checked their problem solution... wow i feel dumb 🤦
This was first question in an interview and I didn't knew the best solution at that time. After coming back I learned about Object method.
Glad it was helpful!
cool video... I noticed two things... First sort: if in the array we have numbers that have more than one digit, in js, sort will be messed up. sort fn should be: sort(a, b) {return a < b} Second when you want to use obj to dedup, the deduped array will be array of strings not array of numbers.
That's what I thought initially, but I think it'll work anyway because the sort will group the duplicates together, even if they're not overall in the right order. I believe the algorithm will still remove the duplicates.
This channel is Lowkey a gold mine. I’m glad I found it. Really simplifies things in such a great way.
Glad you enjoy it! Thanks for watching buddy
Man I would like to thank you for this channel. I'm really enjoy javascript but I don't feel confident enough to go to a job interview ... I know fpr sure that this and you other channel will boost my confidence thnx you in advance sir
No one ever feels confident enough for an interview, even after years of experience. Just go do it!
Way back I had watch this video to learn to remove duplicate items and now in the interview I got this same question. Initially I show him by using indexOf method and again he was not satisfied with this method saying I will have to push the item to the array everytime so this will consume more time. So I again show him the object property method like you have explain in this video and return the Object.keys(obj) . he was very happy. More couple of question and now I got selected in technical round. Thank you @techshit @interviewnest
Somehow this channel was not subscribed. I was so unlucky. I subscribed it now.
I was just asked this question in the interview recently!! Thank you so much for this awesome tutorial!!
I am glad it helped. Thanks for watching!
Man....that was so clean and neat.... understood all of em
Glad to hear it!
Can you please cover some Javascript design patterns like Module, Factory and Prototype?
Using the Object.keys(obj) way returns string values and you begin with numbers, so shouldn't the interviewer want the values back in the same data type ?
I had the same remark while watching the video 😁 and I think that it's not going to be faster because you'll need to do another loop to convert them into numbers
@@zakachou its fine until we use === operator. otherwise yes its true that is slow again.
he could've done obj[i] = i, then use Object.values()
let b = Object.keys(obj).map(x => x * 1);
You can add Number before the element
Actually, using
let len = a.length;
for (let i = 0; i < len; i++) { }
is **slower** than simply writing
for (let i = 0; i < a.length; i++) { }
Test it yourself using jsperf. The runtime engine already allocates a variable for length, so doing the same thing manually is only adding to the performance cost.
You are explaining the concept in a very good manner.... thank you so much :)
Your Style of showing several solutions is super helpful. Thanks for that and keep up.
Great variations with explanations. This is a very common interview question and has always frustrated me! Thanks!
Dude. Never spread a Set before. Friggin' awesome!!
Any reason??
could you please help with any link
that thing you did at 9:18 is f****** amazing!
your videos are always greate, you are a good theacher. respect!!!
I love your videos, thank you for your time and effort. Removing a duplicate element from an array is a tricky thing.By using collection(in Javascript it is Set) it can be done easily.
can you explain this code , bhut help hogi mere liye
instead of creating variable for length of the array outside we can do like this, inside for loop
for (let i=0,j=a.length;i
Its real helpful for the script developer thanks for such a nice explanation
Very Tnks... Good solutions and get best solution step by step👍thats grate
Most welcome
Good explanation with different approach.
Your explanation is amazing.
What a tricky question and answer. I become a fan of yours.
Hey, I love your tutorials and the way you explain everything line by line. Liked, Subscribed and already shared your tutorials with my colleagues. Looking forward for your upcoming videos.
Just noticed,
Object.keys method provided all the string values in the array.
Your videos are awesome, techsith. I always use them as reference when I encounter a new problem in code.
Wow! wonderfully explained, Thanks a lot.
Your videos explain things SO WELL. Thank you so much!
NIce explanation but the issue with Object approach is that you get strings as an array element back.
Good catch.
The only way I see of fixing that is to loop through "b[ ]" and parseInt( ) each member of the array. It's a shame that you can't include type conversion as an argument in the Object.keys( ) method.
In which case, you're better off just looping through the object and pushing each key to a new array while using parseInt, rather than using Object.keys and looping through it again to change the strings to integers.
let b = [ ];
for( key in obj ){
b.push( parseInt( key ) );
}
@@ajasen You can still use the object pattern, it's just a bit messier. At the least, you need an extra conditional or two. And the more edge cases, the messier the code gets. This isn't to say that you would actually want to use this approach, but it can be done for what you're asking. I'll put the code for the cases you provided below. I added a couple of extra cases for "false" or "undefined", vs false or undefined (string value vs boolean or undefined data type in these cases).
P.S. For a Function or Object or BigInt, etc... type as a member of the original array, I would need more logic in my convertToType( ) function, but you get the idea here...
const a = [1, 2, "52634", 1.2, "false" ,"1", "dog", false, false, 4.5, 5.5, 1.2, 5, "cat", "cat", true, null, undefined, undefined, 2, true, null, "null", false, null, 1, 8];
function eliminateDups(theArray) {
let obj = {};
// I threw in a statement for distinguishing edge cases of strings that could be defined as other types such as "undefined" the string vs undefined the data type or "4" vs 4.
for (const key of theArray) {
if(typeof key === "string"){
obj["%String%"+key] = true;
} else {
obj[key] = false;
}
}
let b = [ ];
//assignment of the temp tests for a string then for a number then converts otherwise;
for( key in obj ){
let temp = (obj[key] === true ? key.slice(8) : ( parseFloat(key) || convertToType(key) ));
b.push( temp );
}
console.log("b: ")
console.log(b);
return b;
}
function convertToType (theKey) {
switch(theKey){
case "null":
return null;
break;
case "undefined":
return undefined;
break;
case "false":
case "true":
return theKey === "true";
break;
default:
return theKey;
}
}
console.log(eliminateDups(a));
Keep posting more tutorials.the way u explain things r easy to understand.so Am expecting moreeee videos
Abhinet, I also have another channel where I post more videos on JS , React, HTML , CSS ect. its called techsithtube
A concise explaination, really appreciate your content - subscribed!
Using the Set and spread operator is a cool solution
It's an very helpful for me.Thank you so much sir...
Really neat thing!!! Please do more such a practical puzzle pieces.
Another one liner could be
let arr = [ 1, 2, 5, 2, 1, 8 ];
arr.filter( (num, index) => !ar.slice(0, index).includes(num) );
Maybe not performatic chaining filter, slice and includes, but at least it is pretty (:
Also it is worth noticing that the third solution (the one that converts array to object and then get the keys) will make the numbers get converted to string. in the final array In other words, we are getting [ "1", "2", "5", "8" ] as the result.
We could use the numbers as values instead of "true" and use Object.values(obj) to get them back in array form:
let arr = [ 1, 2, 5, 2, 1, 8 ];
let obj = {}
arr.forEach( num => obj[num] = num )
Object.values(obj) // this returns [ 1, 2, 5, 8 ]
Thank yiu bro
Nice video thx for making this video plz make more videos like this. Very informative and easy to understand for the biggners ❤👌plz make more videos on interview questions so that people who do not gave interviews yet and this is the first time going for the interview these questions are very helpful for them ❤❤
Subscribed straight away!! This is great stuff! Thank you so much
The Complexity of quicksort is O(n * log n) not O(log n)
And that's the BEST-case scenario
@@johnkeck big-O is worst case. best and avg to big-omega and big-?
is the complexity mean how bad/good the code is ? nd these O nd omegas are like the degree of how bad/good is it ?
ps : I have no idea, just wanted a quickflash clue
@@usmaness It's a way to evaluate how much the time will scale with n for a given algorithm.
Indeed gotta pay attention: remove duplicates from array !== create an array of unique values.
you could simplify your loop and just do `for (let i=0, len=a.length; i < len; i++)...
Awesome video as always, thanks for the multiple explanations!
Thanks for watching Adam. Keep on learning!
Hey a small correction, the complexity for quick sort is nlogn and regarding the a length it doesn't calculates the length every time but is stored and whenever you update values js does it automatically.
7:42 quicksort has n*log(n)
You explain concepts so well always! and thank you for mentioning the complexity difference. Very informative! and I saw your closures video but it was somewhat confusing. It would be great if you can explain in more depth with different example
can you explain this code , bhut help hogi mere liye
Bro you are really making sense.. love all your videos.. Big Thanks...
Thanks for the wonderful concepts. Everytime I watch your video, I always learn something new.
Do you have an elaborate video on sets and it's work around?
On monday i will release a video n maps and weakmap data structures.
this would be on my techsithtube channel . I will cover sets and weaksets in next. video.
neat i figured you had to convert to a set, then do the spread but i like that i kind of came up with something similar to the one line
remove duplicates from array in Ruby:
array.uniq
I was there to comment something like "You now there is a problem with js when you see a 12 minutes video explaining how to remove duplicates elements from an array".
But your comment do the job well enough ^^
Sure, but this is even slower than doing it in Python (and I thought nothing could be slower than Python, before I heard about Ruby). Ruby is utter rubbish.
@ remove duplicates from array in Rust :
array.into_iter().dedup() // if array is sorted
array.into_iter().unique() // if not
@@arthurl7139 Rust is nice, I approve.
An alternate way to use an object is like this:
const a = [1, 2, 5, 2, 1, 8];
const b = [];
const arrValMap = {};
for (const i of a) {
if (!arrValMap[i]) {
b.push(i);
arrValMap[i] = true;
}
}
With this approach, the array values remain numbers rather than being converted to strings.... And I just noticed this video is several years old. Oh well.
In the first algorithm, you have the variable len defined globally, which is unnecessary (the same reason why we put let i = 0). It's better to write the for loop as this:
for(let i = 0, len = a.length; i < len; i++)
This way, a.length is accessed only once, which is good, but len is cleared from memory the moment the loop finishes.
Great video, liked and subscribed....very clear, precise and the explanations are great. Thanks. Going to look at more of your work now 👍
Please, make more videos like this one. Thank you.
More to come!
@@InterviewNest if you are interested to do JS interview with me for your channel, my email is milanpjevic@gmail.com
Awesome! can you do a video on best practices? In this video you mentioned that create a separate variable for a.lenght instead of using in the for loop. Similar to that, can you make a separate video on best practices in javascript or any other programming languages?
var array = [1, 1, 2, 3, 4, 4, 5];
var filtered = [];
for (var i = 0; i < array.length; i++) {
if (!filtered.includes(array[i])) {
filtered.push(array[i])
}
}
console.log(filtered)
still N^2 complexity
@@danialmalik80 yup, because 'includes' or 'indexOf' is essentially a for-loop that checks for the existence of an item (number in this case). Therefore, there is an outer loop (for-loop) and an inner loop ('includes) -> N^2
Thank you very much! very good tempo and explanations.
Very constructive and good to follow tutorial! (From great teacher.)
Using the JavaScript Objects method doesn't return an array of NUMBERS. It's actually an array of strings. example: ["1", "3", "5", "8"]
i like your approach with different methods :)
nice video. instructive and simple.
Thanks for the answer sir... marking this question as i have seen it ...also really great how you explained the different ways i which we can achieve the answer...and what's best... my immediate approach though was similar to the brute force method...sat down with the problem and thought of other JS specific way i could solve it... been preparing myself for upcoming interviews and your videos are a great help for it ..thank you
can you explain this code , bhut help hogi mere liye
It shd be noted that the keys of an object are going to be strings. So by using JS objects, there is going to be a type change unintentionally
Great for arrays of primitive types. What about arrays of objects when you want to exclude objects that are equal by content?
damn wish i found you sooner and that i should've studied more. Gonna watch more of your videos and hopefully i'll get better at coding. Thank you so much for these.
Sir this is awesome . thank you so much sir for covering this important question.
md, keep up the good work and keep practicing!
very good content! learning a lot.
Thank you so much for explaining it so well with all possible solutions and the best one.
sir please make more interview programming question ,this helps me confident in the interview
please share your interview question.i doing struggle for getting job
Amazing content! 👏🏻👏🏻👏🏻
You are genius man! Ofc i subscribe on you. Keep going shoot you tutorial. Thank you for you programmers community service!
Great content as always. Just one thing, Can you specify going forward which complexity are you talking about (time complexity or space complexity) because, it sounded like space complexity when you said 2 arrays, and n square, but we only had 1 loop so the time complexity should be only O(n) and not O(n^2) for brute force
it also had .includes which loops over the array
Very good explanation
I need more of these videos
Thank you so much for your explanation, it was SO clear!! It's difficult to find this type of explanations so THANK YOU :)
Only the last method (Set) works for falsy values like null and undefined. So that should be the correct and most complete answer here.
The first example is my favorite because I had already tried to solve this algorithm using a similar method but failed. the problem with my original attempt was that it only looked at the number previous to current selection. So there were still duplicates in the array.
3rd solution converts numbers to string that might trigger more questions in the interview
when we are using Object.keys approach, it returns the string values as the elements, hence we again need to convert this to an integer using the array.map, which in turns increases the complexity
let a = Object.keys(obj).map((value)=>{
return parseInt(value, 10);
})
Cool trick! you earned my subscribe sir! looking forward for more knowledge and tricks!
Nice tutorial! It has a lot of information!!!
Any idea why I'm getting any empty array because I'm trying to convert the array elements to integers each using the parseInt method?
let a = [8,7,7,8,1,2,3,4,3,5,6,1,2,1,2,3];
let obj = {};
for(let i of a) {
obj[ i ] = true;
}
let b = Object.keys(obj);
console.log(Array.from(parseInt(b))); // this returns an empty array `[ ]`
excellent pedagogy, thank you sir ! :)
Thanks. Learnt new things and got new ideas.
how will you achieve the same using for loop without any other variables??
nicely explained, keep it up sir, thank you 👍👍👍👍🙂🙂🙂🙂
Wouldn't the complexity of the brute force method be just O(N) seeing as you're looping through the array just once and there are no nested loops?
Depending on which approach you take. If you sort it before looping through than it s O(nlogn) because best sorting algo takes that complexity
instead of above concept we can use console.log([...new Set(nums)]) will return the unique value. for ascending and descending we can use reverse and sort can use.. your comment required here Sir
Santosh, yes that is also a simplest way to remove duplicate values from an array.
Sort() function sometimes does work good with number you need to write comparison function like a.sort(function (a,b)=> a-b)
Thank you
yup, it will not work for something like [1, 11, 2, 3, 33, 4].
For this purpose, it doesn't matter, so long as all identical elements are next to each other. [1,1,1,11,11,2,23,23,3,3,3,33,4] would work because we're only pushing a new item to the resulting array "b", if the previous item wasn't the same as the current one.
The complexity of a quick sort is n*log(n). Also I’m not sure if JS objects can have say, millions of elements?
The LeetCode specified that "Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory."
Great work man. One request, can you please add videos on datatypes fromJS, Map, List, Set (import { fromJS, Map, List, Set } from 'immutable' )
On monday i will release a video n maps and weakmap data structures. this would be on my techsithtube channel . I will cover sets next.
InterviewNest thx
Last solution also respects the order!
In my interview I was asked to sort alphanumeric array [5,8,az,bt,68,gy,15]
I am assuming i was [5,8,"az","bt",68,"gy",15]
[5,8,"az","bt",68,"gy",15].sort()
Awesome tutorial thank you so much!
Great explanation!! Can you please upload a video on lazy loading using plain javascript
nice sir pls one question any array [1,2,3,[a,b,c,[1,2,3]]] to change a nested array to flat array please give a video
Of course there is filter you can use as well.
let b = a.filter( val, idx, arr) => arr.indexOf(val) === idx;