Skip to content

Commit 4042709

Browse files
committed
feat: Add functions for sorting, finding max/min, sum/average, prime/palindrome checks, and duplicate removal
- Add functions to sort an array of numbers, find the maximum and minimum numbers in an array, find duplicate numbers in an array, remove duplicate numbers from an array, check if a number is prime, check if a string is a palindrome, reverse a string, and reverse a number. - Implement the functions without using any built-in methods.
1 parent d3ceee0 commit 4042709

File tree

1 file changed

+45
-31
lines changed

1 file changed

+45
-31
lines changed

README.md

+45-31
Original file line numberDiff line numberDiff line change
@@ -7061,10 +7061,10 @@ JavaScript offers several shorthand techniques to write code more concisely and
70617061
70627062
## Interview Questions
70637063
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.
70657065
70667066
```javascript
7067-
function sortArray(arr) {
7067+
const sortArray = (arr) => {
70687068
for (let i = 0; i < arr.length; i++) {
70697069
for (let j = i + 1; j < arr.length; j++) {
70707070
if (arr[i] > arr[j]) {
@@ -7075,41 +7075,41 @@ function sortArray(arr) {
70757075
}
70767076
}
70777077
return arr;
7078-
}
7078+
};
70797079

70807080
const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
70817081
console.log(sortArray(numbers)); // Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
70827082
```
70837083
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.
70857085
70867086
```javascript
7087-
function findMax(arr) {
7087+
const findMax = (arr) => {
70887088
let max = arr[0];
70897089
for (let i = 1; i < arr.length; i++) {
70907090
if (arr[i] > max) {
70917091
max = arr[i];
70927092
}
70937093
}
70947094
return max;
7095-
}
7095+
};
70967096

70977097
const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
70987098
console.log(findMax(numbers)); // Output: 9
70997099
```
71007100
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.
71027102
71037103
```javascript
7104-
function findMin(arr) {
7104+
const findMin = (arr) => {
71057105
let min = arr[0];
71067106
for (let i = 1; i < arr.length; i++) {
71077107
if (arr[i] < min) {
71087108
min = arr[i];
71097109
}
71107110
}
71117111
return min;
7112-
}
7112+
};
71137113

71147114
const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
71157115
console.log(findMin(numbers)); // Output: 1
@@ -7199,76 +7199,90 @@ console.log(isPalindrome('madam')); // Output: true
71997199
console.log(isPalindrome('hello')); // Output: false
72007200
```
72017201
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.
72037203
72047204
```javascript
7205-
function findDuplicates(arr) {
7206-
const duplicates = [];
7205+
const findDuplicates = (arr) => {
7206+
let duplicates = [];
7207+
let index = 0;
72077208
for (let i = 0; i < arr.length; i++) {
72087209
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+
}
72117222
}
72127223
}
72137224
}
72147225
return duplicates;
7215-
}
7226+
};
72167227

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]
72197230
```
72207231
72217232
[Back to Top⤴️](#table-of-contents)
72227233
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.
72247235
72257236
```javascript
7226-
function removeDuplicates(arr) {
7227-
const unique = [];
7237+
const removeDuplicates = (arr) => {
7238+
let unique = [];
7239+
let index = 0;
7240+
72287241
for (let i = 0; i < arr.length; i++) {
72297242
let isDuplicate = false;
7230-
for (let j = 0; j < unique.length; j++) {
7243+
for (let j = 0; j < index; j++) {
72317244
if (arr[i] === unique[j]) {
72327245
isDuplicate = true;
72337246
break;
72347247
}
72357248
}
72367249
if (!isDuplicate) {
7237-
unique.push(arr[i]);
7250+
unique[index] = arr[i];
7251+
index++;
72387252
}
72397253
}
72407254
return unique;
7241-
}
7255+
};
72427256

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]
72457259
```
72467260
7247-
Write a function to reverse a string Without Inbuilt Method.
7261+
Reverse a string without using any built-in method.
72487262
72497263
```javascript
7250-
function reverseString(str) {
7264+
const reverseString = (str) => {
72517265
let reversed = '';
72527266
for (let i = str.length - 1; i >= 0; i--) {
72537267
reversed += str[i];
72547268
}
72557269
return reversed;
7256-
}
7270+
};
72577271

72587272
console.log(reverseString('hello')); // Output: 'olleh'
72597273
```
72607274
7261-
Write a function to reverse a number Without Inbuilt Method.
7275+
Reverse a number without using any built-in method.
72627276
72637277
```javascript
7264-
function reverseNumber(num) {
7278+
const reverseNumber = (num) => {
72657279
let reversed = 0;
72667280
while (num > 0) {
72677281
reversed = reversed * 10 + (num % 10);
72687282
num = Math.floor(num / 10);
72697283
}
72707284
return reversed;
7271-
}
7285+
};
72727286

72737287
console.log(reverseNumber(12345)); // Output: 54321
72747288
```

0 commit comments

Comments
 (0)