Arrays

Run Settings
LanguageJavaScript
Language Version
Run Command
/* lookup : O(1) push : O(1). // always insert at last insert : O(n) delete : O(n) pop : O(1) // always delete from last */ const arr = ['a','b','c','d'] /* * In arr lookups are fast so if we already no the index then we can directly access it and that's why TC for arr look up is O(1). */ console.log(arr[1]) /* * With push method we can add element at the end of the arr and as we are adding at the end of arr it does not effect on other indexes and that's why TC of push method is O(1). */ arr.push('e') // OUTPUT : ['a','b','c','d','e'] /* * With pop method we can remove element from the end of the arr and as we are removing from the end of arr it does not effect on other indexes and that's why TC of pop method is O(1). */ arr.pop() // OUTPUT : ['a','b','c','d'] /* * With unshift method we add element at first index . Now as we are adding at first index all other indexes has to move and that's why TC of unshit is O(n). */ arr.unshift('x'); // OUTPUT : ['x','a','b','c','d'] /* * With shift method we can remove element from first index . Now as we are removing from first index all other indexes has to move and that's why TC of unshit is O(n). */ arr.shift() ; // OUTPUT : [a','b','c','d'] /* * With splice method we can add value in between arr and this is useful method that i was not yet aware about. * params : startIndex : Index in which you want to add value deleteCount : after that index if you want of delete any number of other elements. item : item that you want to add. * now TC for splice is also O(n) as it does affect on other index and we need to shift other indexes to add or remove elements inbetween array. */ arr.splice(1,1,"haresh")//it will add haresh after a and will remove b as we are passing count 1 if we pass count 2 as second param then will remove b and c. console.log(arr) /**************** STATIC AND DYNAMIC ARRAY **********************/ /* * Now this is something we learned in java like static array where you define size of array and can add only that much elemenets in array. So example int a = new int[5] suppose i created arr like this so what that mean is we can store only 5 values into that array means java is going to create 5 blocks where we can store each elements now in this case only issue is that we can not add any new elements when our arr gets full. * Now above thing we talked about java only BUT IN JAVASCRIPT WE ONLY HAVE DYNAMIC ARR MEANS WE DON'T NEED TO WORRY ABOVE MEMORY STUFF and that's why we have never given size to array in javascript. * One last thing in some cases TC of push method can be O(n) and that is when our array is full and js internal tries to copy that array and create new array with double size. (GOOD TO KNOW HOW IT INTERNAL WORK) */ /**************. CLASSES IN JS (OPTIONAL VIDEO) ***********************/ // IN THIS VIDEO WE HAVE LEARNED THREE TOPIC /* * REFERANCE TYPE : I HAVE GOOD UNDERSTANDING OF IT FROM JAVA (Primitive type and Referance type) * Below one is best and most know example to understand referance type. * All we need to understand that object does not compare values object compares address and thats why those are referance type it does not important that what value we have in obj but where they stored metter in below example. * IN SIMPLE WORD OBJECTS COMPARE REFERANCE NOT VALUE. */ let obj1 = {value : 10} let obj2 = obj1 let obj3 = {value : 10} console.log(obj1 === obj2) console.log(obj1 === obj3) /* Context : This is something we need to learn more about in javascript context simply means "this" keyword at global level window === this. * NOW best way to know value of this is we need to understand whats is the left side of that like where you are accessing this. LEARN BY EXAMPLE BUT REALLY ITS EASY ONLY IF YOU UNDERSTAND WELL. */ console.log(this) // NOW THIS BROWSER NOT SHOWING WINDOW OBJECT BUT UNDERSTAND IF WE ARE ACCESSING THIS AT GLOBAL LEVEL THIS this === window function stillThisIsWindow () { console.log("still this is equal to global",this === global) } /* * Now here is learning part even inside function why we are getting this === global true and that is because left side of function is global like we are access this function in global enviroment. MUST UNDERSTAND THIS concept * And real use of this will be when you are using objects. (This sentance you need to understand) */ stillThisIsWindow() let objForThis = { name : "Haresh", age : 26, work : function () {console.log("abi this ki value change hogi ",this)} } objForThis.work() // SEE NOW TO ACCESS WORK WE ARE USING objForThis and left side of work is objForThis and that's why value of this is that entire object. /* * Class : For this also i have good idea from java and in this course we will use a lot so no need to do anything just one example */ class JOB { constructor(name,exp) { this.name = name; this.exp = exp } intro() { console.log(`My name is ${this.name} and i have ${this.exp} in javascript`) } } class ITJOB extends JOB { constructor(name,exp){ super(name,exp) this.name = name this.exp = exp } salary(){ console.log(`${this.name} will get position of sde2 because you have ${this.exp} years of exp`) } } // creating instance of class let itJob = new ITJOB("RAGHU",7) itJob.salary()//calling child class method itJob.intro()//calling parent class method.Still "this" will be ITJOB because left side we have ITJOB // THATS ALL ABOUT REFERNACE TYPE , CONTEXT AND INSTANTIATION MOVE TO DSA AGAIN MAN
Editor Settings
Theme
Key bindings
Full width
Lines