@@ -7061,10 +7061,10 @@ JavaScript offers several shorthand techniques to write code more concisely and
7061
7061
7062
7062
## Interview Questions
7063
7063
7064
- Write a function that sorts an array of numbers Without Inbuilt Method .
7064
+ Sorts an array of numbers without using any built-in method .
7065
7065
7066
7066
` ` ` javascript
7067
- function sortArray (arr ) {
7067
+ const sortArray = (arr ) => {
7068
7068
for (let i = 0 ; i < arr .length ; i++ ) {
7069
7069
for (let j = i + 1 ; j < arr .length ; j++ ) {
7070
7070
if (arr[i] > arr[j]) {
@@ -7075,41 +7075,41 @@ function sortArray(arr) {
7075
7075
}
7076
7076
}
7077
7077
return arr;
7078
- }
7078
+ };
7079
7079
7080
7080
const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7081
7081
console .log (sortArray (numbers)); // Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
7082
7082
` ` `
7083
7083
7084
- Write a function that finds the maximum number in an array of numbers Without Inbuilt Method .
7084
+ Find the maximum number in an array of numbers without using any built-in method .
7085
7085
7086
7086
` ` ` javascript
7087
- function findMax (arr ) {
7087
+ const findMax = (arr ) => {
7088
7088
let max = arr[0 ];
7089
7089
for (let i = 1 ; i < arr .length ; i++ ) {
7090
7090
if (arr[i] > max) {
7091
7091
max = arr[i];
7092
7092
}
7093
7093
}
7094
7094
return max;
7095
- }
7095
+ };
7096
7096
7097
7097
const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7098
7098
console .log (findMax (numbers)); // Output: 9
7099
7099
` ` `
7100
7100
7101
- Write a function that finds the minimum number in an array of numbers Without Inbuilt Method .
7101
+ Find the minimum number in an array of numbers without using any built-in method .
7102
7102
7103
7103
` ` ` javascript
7104
- function findMin (arr ) {
7104
+ const findMin = (arr ) => {
7105
7105
let min = arr[0 ];
7106
7106
for (let i = 1 ; i < arr .length ; i++ ) {
7107
7107
if (arr[i] < min) {
7108
7108
min = arr[i];
7109
7109
}
7110
7110
}
7111
7111
return min;
7112
- }
7112
+ };
7113
7113
7114
7114
const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7115
7115
console .log (findMin (numbers)); // Output: 1
@@ -7199,76 +7199,90 @@ console.log(isPalindrome('madam')); // Output: true
7199
7199
console .log (isPalindrome (' hello' )); // Output: false
7200
7200
` ` `
7201
7201
7202
- Write a function to find duplicate numbers in an Array Without Inbuilt Method .
7202
+ Find duplicate numbers in an array without using any built-in method .
7203
7203
7204
7204
` ` ` javascript
7205
- function findDuplicates (arr ) {
7206
- const duplicates = [];
7205
+ const findDuplicates = (arr ) => {
7206
+ let duplicates = [];
7207
+ let index = 0 ;
7207
7208
for (let i = 0 ; i < arr .length ; i++ ) {
7208
7209
for (let j = i + 1 ; j < arr .length ; j++ ) {
7209
- if (arr[i] === arr[j] && ! duplicates .includes (arr[i])) {
7210
- duplicates .push (arr[i]);
7210
+ let isDuplicate = false ;
7211
+ if (arr[i] === arr[j]) {
7212
+ for (let k = 0 ; k < index; k++ ) {
7213
+ if (duplicates[k] === arr[i]) {
7214
+ isDuplicate = true ;
7215
+ break ;
7216
+ }
7217
+ }
7218
+ if (! isDuplicate) {
7219
+ duplicates[index] = arr[i];
7220
+ index++ ;
7221
+ }
7211
7222
}
7212
7223
}
7213
7224
}
7214
7225
return duplicates;
7215
- }
7226
+ };
7216
7227
7217
- const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7218
- console .log (findDuplicates (numbers)); // Output: [1 , 3, 5 ]
7228
+ let numbers = [1 , 2 , 3 , 4 , 2 , 5 , 6 , 3 , 7 , 8 , 8 ];
7229
+ console .log (findDuplicates (numbers)); // Output: [2 , 3, 8 ]
7219
7230
` ` `
7220
7231
7221
7232
[Back to Top⤴️](#table-of-contents)
7222
7233
7223
- Write a function to remove duplicate numbers in an Array Without Inbuilt Method .
7234
+ Remove duplicate numbers in an array without using any built-in method .
7224
7235
7225
7236
` ` ` javascript
7226
- function removeDuplicates (arr ) {
7227
- const unique = [];
7237
+ const removeDuplicates = (arr ) => {
7238
+ let unique = [];
7239
+ let index = 0 ;
7240
+
7228
7241
for (let i = 0 ; i < arr .length ; i++ ) {
7229
7242
let isDuplicate = false ;
7230
- for (let j = 0 ; j < unique . length ; j++ ) {
7243
+ for (let j = 0 ; j < index ; j++ ) {
7231
7244
if (arr[i] === unique[j]) {
7232
7245
isDuplicate = true ;
7233
7246
break ;
7234
7247
}
7235
7248
}
7236
7249
if (! isDuplicate) {
7237
- unique .push (arr[i]);
7250
+ unique[index] = arr[i];
7251
+ index++ ;
7238
7252
}
7239
7253
}
7240
7254
return unique;
7241
- }
7255
+ };
7242
7256
7243
- const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7244
- console .log (removeDuplicates (numbers)); // Output: [3, 1, 4, 5, 9, 2, 6 ]
7257
+ let numbers = [1 , 2 , 3 , 4 , 2 , 5 , 6 , 3 , 7 , 8 , 8 ];
7258
+ console .log (removeDuplicates (numbers)); // Output: [1, 2, 3, 4, 5, 6, 7, 8 ]
7245
7259
` ` `
7246
7260
7247
- Write a function to reverse a string Without Inbuilt Method .
7261
+ Reverse a string without using any built-in method .
7248
7262
7249
7263
` ` ` javascript
7250
- function reverseString (str ) {
7264
+ const reverseString = (str ) => {
7251
7265
let reversed = ' ' ;
7252
7266
for (let i = str .length - 1 ; i >= 0 ; i-- ) {
7253
7267
reversed += str[i];
7254
7268
}
7255
7269
return reversed;
7256
- }
7270
+ };
7257
7271
7258
7272
console .log (reverseString (' hello' )); // Output: 'olleh'
7259
7273
` ` `
7260
7274
7261
- Write a function to reverse a number Without Inbuilt Method .
7275
+ Reverse a number without using any built-in method .
7262
7276
7263
7277
` ` ` javascript
7264
- function reverseNumber (num ) {
7278
+ const reverseNumber = (num ) => {
7265
7279
let reversed = 0 ;
7266
7280
while (num > 0 ) {
7267
7281
reversed = reversed * 10 + (num % 10 );
7268
7282
num = Math .floor (num / 10 );
7269
7283
}
7270
7284
return reversed;
7271
- }
7285
+ };
7272
7286
7273
7287
console .log (reverseNumber (12345 )); // Output: 54321
7274
7288
` ` `
0 commit comments