|
| 1 | +#Array Manipulation in #"diff-5dd02a5489b8804f74afbcb5673c28fef853bdaf3e0e5edce72f9c2da728941e-empty-2-0" data-selected="false" role="gridcell" tabindex="-1" valign="top"> | 2 | + |
| 3 | +###How do you create an empty array in JavaScript? |
| 4 | +```javascript |
| 5 | +constarr= [1,2,3,4,"Hello", {name:"Vishal"}, [1,2,3],4]; |
| 6 | +// const arr2 = new Array(); |
| 7 | +console.log(arr); |
| 8 | +``` |
| 9 | + |
| 10 | +###How do you access the first and last elements of an array? |
| 11 | +```javascript |
| 12 | +constfirstElement= arr[0];// O(1) |
| 13 | +constarrLength=arr.length; |
| 14 | +constlastElement= arr[arrLength-1]; |
| 15 | +console.log(firstElement, arrLength, lastElement); |
| 16 | +``` |
| 17 | + |
| 18 | +###How do you remove the last element from an array? |
| 19 | +```javascript |
| 20 | +constlastElement1=arr.pop();// O(1) |
| 21 | +console.log(arr, lastElement1); |
| 22 | +``` |
| 23 | + |
| 24 | +###How do you add an element to the end of an array? |
| 25 | +```javascript |
| 26 | +arr.push(5);// O(1) |
| 27 | +console.log(arr); |
| 28 | +``` |
| 29 | + |
| 30 | +###How do you add an element to the start of an array? |
| 31 | +```javascript |
| 32 | +arr.unshift(0);// O(N) |
| 33 | +console.log(arr); |
| 34 | +``` |
| 35 | + |
| 36 | +###How do you remove the first element from an array? |
| 37 | +```javascript |
| 38 | +arr.shift();// O(N) |
| 39 | +console.log(arr); |
| 40 | +``` |
| 41 | + |
| 42 | +###How do you loop through an array in JavaScript? |
| 43 | +```javascript |
| 44 | +for (let i=0; i<arr.length; i++){ |
| 45 | +console.log(arr[i]); |
| 46 | +} |
| 47 | + |
| 48 | +arr.forEach((x,i)=> { |
| 49 | +console.log(x); |
| 50 | +}); |
| 51 | + |
| 52 | +for (let xof arr){ |
| 53 | +console.log(x); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +###Question 1: How do you check if an element exists in an array? |
| 58 | +```javascript |
| 59 | +constfindElement= (arr,target)=> { |
| 60 | +for (let xof arr){ |
| 61 | +if (x=== target){ |
| 62 | +returntrue; |
| 63 | + } |
| 64 | + } |
| 65 | +returnfalse; |
| 66 | +} |
| 67 | + |
| 68 | +console.log(findElement(arr,"Hello")); |
| 69 | +console.log(findElement(arr,"H")); |
| 70 | +console.log(arr.includes("Hello")); |
| 71 | +``` |
| 72 | + |
| 73 | +###Question 2: How do you find the index of an element in an array? |
| 74 | +```javascript |
| 75 | +constfindElementIndex= (arr,target)=> { |
| 76 | +for (let i=0; i<arr.length; i++){ |
| 77 | +if (arr[i]=== target){ |
| 78 | +return i; |
| 79 | + } |
| 80 | + } |
| 81 | +return-1; |
| 82 | +} |
| 83 | + |
| 84 | +console.log(findElementIndex(arr,"Hello")); |
| 85 | +console.log(arr.indexOf("Hello")); |
| 86 | +``` |
| 87 | + |
| 88 | +###How to delete, add & update elements from a specific index? |
| 89 | +```javascript |
| 90 | +console.log(arr); |
| 91 | +arr.splice(1,3); |
| 92 | +console.log(arr); |
| 93 | +arr.splice(1,0,2,3,4,5,6); |
| 94 | +console.log(arr); |
| 95 | +arr.splice(1,3,6,7,8); |
| 96 | +console.log(arr); |
| 97 | +``` |
| 98 | + |
| 99 | +###`splice()` vs`slice()` |
| 100 | +```javascript |
| 101 | +constsubArr=arr.slice(1,4);// [start, end) |
| 102 | +console.log(subArr); |
| 103 | +``` |
| 104 | + |
| 105 | +###Shallow Copy of Array |
| 106 | +```javascript |
| 107 | +constarrB= arr; |
| 108 | +arrB.splice(1,4); |
| 109 | +console.log(arrB, arr); |
| 110 | +``` |
| 111 | + |
| 112 | +###Deep Copy of Array |
| 113 | +```javascript |
| 114 | +constarrC= [...arr]; |
| 115 | +constarrD=Array.from(arr); |
| 116 | +constarrE=arr.concat(); |
| 117 | +arrC.splice(1,4); |
| 118 | +arrD.splice(1,4); |
| 119 | +arrE.splice(1,3); |
| 120 | +console.log(arrC, arrD, arrE, arr); |
| 121 | +``` |
| 122 | + |
| 123 | +###How to concatenate two arrays in JavaScript? |
| 124 | +```javascript |
| 125 | +constnewArr= [...arr,...arrE]; |
| 126 | +constnewArr2=arr.concat(arrE); |
| 127 | +console.log(newArr, newArr2); |
| 128 | +``` |
| 129 | + |
| 130 | +###Question 3: How can you check if two arrays are equal? |
| 131 | +```javascript |
| 132 | +constisArrayEqual= (arr1,arr2)=> { |
| 133 | +if (arr1.length!==arr2.length){ |
| 134 | +returnfalse; |
| 135 | + } |
| 136 | + |
| 137 | +for (let i=0; i<arr1.length; i++){ |
| 138 | +if (arr1[i]!== arr2[i]){ |
| 139 | +returnfalse; |
| 140 | + } |
| 141 | + } |
| 142 | +returntrue; |
| 143 | + |
| 144 | +// One Line solution |
| 145 | +// return arr1.length === arr2.length && arr1.every((ele, i) => arr1[i] === arr2[i]); |
| 146 | +} |
| 147 | + |
| 148 | +console.log(isArrayEqual([1,2,3], [1,2,3])); |
| 149 | +``` |
| 150 | + |
| 151 | +###Question 4: How to sort an array in ascending and descending order? |
| 152 | +```javascript |
| 153 | +constx= [1,4,6,0,-9,-5]; |
| 154 | +x.sort();// O(NlogN) |
| 155 | +console.log(x); |
| 156 | + |
| 157 | +x.sort((a,b)=> b- a); |
| 158 | +console.log(x); |
| 159 | +``` |
| 160 | + |
| 161 | +###Question 5: How to reverse an array? |
| 162 | +```javascript |
| 163 | +x.reverse(); |
| 164 | +console.log(x); |
| 165 | +``` |
| 166 | + |
| 167 | +###Map, Filter & Reduce |
| 168 | +```javascript |
| 169 | +constnewMapArr=x.map((ele,i)=> ele* ele); |
| 170 | +console.log(newMapArr); |
| 171 | + |
| 172 | +constpositiveNumbers=x.filter((ele,i)=> ele>0); |
| 173 | +console.log(positiveNumbers); |
| 174 | + |
| 175 | +constsumOFArr=positiveNumbers.reduce((acc,ele)=> acc+ ele); |
| 176 | +console.log(sumOFArr); |
| 177 | +``` |
| 178 | + |
| 179 | +###Flat:[1, 2, 4, 5, 6, 7, 8, 9] |
| 180 | +```javascript |
| 181 | +consty= [1,2, [4,5, [6,7]],8,9]; |
| 182 | +constflattedArray=y.flat(2); |
| 183 | +console.log(flattedArray); |
| 184 | +``` |
| 185 | + |
| 186 | +###`filter()` vs`find()` |
| 187 | +```javascript |
| 188 | +constpositiveNumber=x.find((ele,i)=> ele>0); |
| 189 | +console.log(positiveNumber); |
| 190 | +``` |
| 191 | + |
| 192 | +#Practice Questions |
| 193 | + |
| 194 | +-[Two Sum](https://leetcode.com/problems/two-sum/) |
| 195 | +-[Majority Element](https://leetcode.com/problems/majority-element/) |
| 196 | +-[Remove Duplicates from sorted array](https://leetcode.com/problems/remove-duplicates-from-sorted-array) |
| 197 | +-[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array) |
| 198 | +-[Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) |
| 199 | +-[Move Zeroes](https://leetcode.com/problems/move-zeroes) |
| 200 | +-[Remove Element](https://leetcode.com/problems/remove-element) |
| 201 | +-[Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) |