Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite6fd223

Browse files
committed
Added Array questions and source code
1 parent09b5413 commite6fd223

File tree

3 files changed

+225
-155
lines changed

3 files changed

+225
-155
lines changed

‎Array/README.md‎

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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/)

‎Basics/README.md‎

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Practise Loop, functions & Math Problems
22

3-
###Question 1:sum of all natural numbers from 1 to n
3+
###Question 1:Sum of all natural numbers from 1 to n
44

55
```javascript
66
functionsumOfNaturalNumber(num){
@@ -11,12 +11,11 @@ function sumOfNaturalNumber(num){
1111
return sum;
1212
}
1313

14-
console.log(sumOfNaturalNumber(5))//15
15-
console.log(sumOfNaturalNumber(10))//55
16-
console.log(sumOfNaturalNumber(8))//36
14+
console.log(sumOfNaturalNumber(5));//15
15+
console.log(sumOfNaturalNumber(10));//55
16+
console.log(sumOfNaturalNumber(8));//36
1717
```
1818

19-
2019
###Question 2: Sum of digits of a number
2120

2221
```javascript
@@ -29,11 +28,10 @@ function sumOfDigits(num){
2928
return sum;
3029
}
3130

32-
console.log(sumOfDigits(1287))//18
31+
console.log(sumOfDigits(1287));//18
3332
```
3433

35-
36-
###Question 3: count the number of digits of a number
34+
###Question 3: Count the number of digits of a number
3735

3836
```javascript
3937
functioncountDigits(num){
@@ -46,31 +44,30 @@ function countDigits(num){
4644
return count;
4745
}
4846

49-
console.log(countDigits(121))//3
50-
console.log(countDigits(-1211413131))//10
47+
console.log(countDigits(121));//3
48+
console.log(countDigits(-1211413131));//10
5149
```
5250

53-
###Question 4: Given an integer x, return true if x is a palindrome, and false otherwise.
54-
A palindrome number is a number that remains the same when digits are reversed
51+
###Question 4: Check if a number is palindrome
5552

5653
```javascript
5754
letisPalindrome=function(x) {
5855
let copyNum= x, reverseNum=0;
5956

6057
while(copyNum>0){
61-
constlastDigit= copyNum%10;
62-
reverseNum= reverseNum*10+ lastDigit;
63-
copyNum=Math.floor(copyNum/10)
58+
constlastDigit= copyNum%10;
59+
reverseNum= reverseNum*10+ lastDigit;
60+
copyNum=Math.floor(copyNum/10);
6461
}
6562

66-
return x=== reverseNum
63+
return x=== reverseNum;
6764
};
6865

69-
console.log(isPalindrome(121))//true
70-
console.log(isPalindrome(1234))//false
66+
console.log(isPalindrome(121));//true
67+
console.log(isPalindrome(1234));//false
7168
```
7269

73-
###Question 5: Find nthfibonacci number
70+
###Question 5: Find nthFibonacci number
7471
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence,
7572
such that each number is the sum of the two preceding ones, starting from 0 and 1.
7673

@@ -88,16 +85,15 @@ let fib = function(n) {
8885
}
8986
return next;
9087
};
88+
9189
// Fibonacci Sequence: 0 1 1 2 3 5 8...
92-
console.log(fib(5))// 5
93-
console.log(fib(10))//55
90+
console.log(fib(5));// 5
91+
console.log(fib(10));//55
9492
```
9593

96-
97-
###Question 6: Missing Number
94+
###Question 6: Missing Number in an Array
9895
Given an array nums containing n distinct numbers in the range[0, n],
9996
return the only number in the range that is missing from the array.
100-
10197
```javascript
10298
letmissingNumber=function(nums) {
10399
let sum=0;
@@ -107,19 +103,16 @@ let missingNumber = function(nums) {
107103
returnnums.length*(nums.length+1)/2- sum;
108104
};
109105

110-
111106
// One Line Solution:
112107
letmissingNumber= (nums)=>nums.length*(nums.length+1)/2-nums.reduce((acc,num)=> num+ acc);
113108

114-
115-
console.log(missingNumber([3,0,1]))//2
116-
console.log(missingNumber([9,6,4,2,3,5,7,0,1]))//8
109+
console.log(missingNumber([3,0,1]));// 2
110+
console.log(missingNumber([9,6,4,2,3,5,7,0,1]));// 8
117111
```
118112

119-
120113
#Practice Questions
121114

122-
-[Countodd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)
115+
-[CountOdd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)
123116
-[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)
124117
-[Power of Two](https://leetcode.com/problems/power-of-two/)
125-
-[Find Square root of a Number](https://leetcode.com/problems/sqrtx/)
118+
-[Find Square root of a Number](https://leetcode.com/problems/sqrtx/)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp