Skip to content

Commit ffb8f56

Browse files
algorithms updated
1 parent c345ee4 commit ffb8f56

File tree

7 files changed

+145
-3
lines changed

7 files changed

+145
-3
lines changed

Arrays-Questions/duplicate-from-array.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
let arr = [1, 2, 1, 3, 2, 1, 2, 3, 4, 5];
22

3+
// O(nSquare) due to .includes
34
const getDuplicateFromArr = (inputArr) => {
45
let resultArr = [];
56
let obj = {};
@@ -15,4 +16,20 @@ const getDuplicateFromArr = (inputArr) => {
1516
return resultArr;
1617
}
1718

18-
console.log(getDuplicateFromArr(arr));
19+
console.log(getDuplicateFromArr(arr));
20+
21+
// O(n) due to Set has O(1)
22+
function getDuplicateFromArrOpt(inputArr) {
23+
const duplicateSet = new Set();
24+
const uniqueSet = new Set();
25+
for (let i = 0; i < inputArr.length; i++) {
26+
if (uniqueSet.has(inputArr[i])) {
27+
duplicateSet.add(inputArr[i]);
28+
} else {
29+
uniqueSet.add(inputArr[i])
30+
}
31+
}
32+
return duplicateSet;
33+
}
34+
35+
console.log(getDuplicateFromArrOpt(arr))

Arrays-Questions/fibonacci-series.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1.
3+
* Formula: F(n)=F(n−1)+F(n−2)
4+
* Example: 0,1,1,2,3,5,8,13,21,34,55,…
5+
* Time Complexity O(n)
6+
*/
7+
8+
const getFibbonaciSeries = ((tillNum) => {
9+
const series = [0, 1]
10+
for (let i = 2; i < tillNum; i++) {
11+
series.push(series[series.length-1] + series[series.length-2])
12+
}
13+
return series
14+
});
15+
16+
console.log(getFibbonaciSeries(10))

Arrays-Questions/insert-ele.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Q. Insert new element at 3rd position
3+
* Time complexity O(n), cause using slice and splice methods due to element shifting
4+
* Ideally linear search
5+
*/
6+
7+
let arr = [1,2,4,5,6];
8+
let leftArr = arr.slice(0,2);
9+
let middleArr = [3]
10+
let rightArr = arr.slice(2)
11+
let resultArr = [];
12+
13+
resultArr.push(...leftArr)
14+
resultArr.push(...middleArr)
15+
resultArr.push(...rightArr);
16+
17+
console.log('slice',resultArr)
18+
19+
// With splice
20+
arr.splice(2,0,3);
21+
console.log('splice one liner',arr)

Arrays-Questions/is-prime-number.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
// Write a program for check number is prime or not ?
1+
/**
2+
* Q. Write a program for check number is prime or not ?
3+
* Only divisible by 1 or SELF only
4+
* @param {number} inputNum
5+
* @returns {boolean}
6+
*/
27
const isPrime = (inputNum) => {
38
let result = true;
49
for (let i = 2; i < inputNum; i++) {

Object-Questions/object-with-map.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Benefits of Map
3+
* 1. Faster with large datasets also
4+
* 2. Ordered keys be default
5+
*/
6+
7+
const map = new Map();
8+
map.set("name", "Sanjay");
9+
map.set("age", 27);
10+
map.set("designation", "Senior Consultant");
11+
12+
console.log(map.get("name"));
13+
14+
// Iteration
15+
map.forEach((value, key) => {
16+
console.log(`${key}: ${value}`);
17+
});
18+
19+
console.log(Object.keys(map)) // Not works as it is Map not Object

Searching-Algos/binary-search.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,27 @@ const binarySearch = (sortedNumArr, findVal) => {
4747
}
4848

4949
console.log(binarySearch([1, 2, 3, 4, 5], 6)) // -1
50-
console.log(binarySearch([1, 3, 5, 6, 9, 14, 29, 57, 89], 29)); // 1
50+
console.log(binarySearch([1, 3, 5, 6, 9, 14, 29, 57, 89], 29)); // 1
51+
52+
let arr = [1, 2, 3, 4, 10, 20, 31, 43, 51]
53+
54+
function inArray(val) {
55+
let result = false;
56+
let leftIndex = 0, rightIndex = arr.length - 1, midIndex;
57+
let count = 0;
58+
while(leftIndex < rightIndex) {
59+
midIndex = Math.round((leftIndex + rightIndex)/2)
60+
if(arr[leftIndex] === val || arr[midIndex] === val || arr[rightIndex] === val) return true
61+
if(midIndex === rightIndex) return false
62+
if(val > arr[midIndex]) {
63+
leftIndex = midIndex
64+
} else {
65+
rightIndex = midIndex
66+
}
67+
count ++
68+
console.log(count)
69+
}
70+
return result;
71+
}
72+
73+
console.log(inArray(43));

Sorting-Algos/bubble-sort.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Custom sorting program in JS via Bubble Sort?
2+
// Time Complexity O(nSquare)
3+
// Space Complexity O(1)
24
let unSortArr = [4, -1, 34, 9, -9, 103];
35

46
const sortArr = (inputArr) => {
@@ -46,3 +48,42 @@ const sortArrOpt = (inputArr) => {
4648
};
4749

4850
console.log(sortArrOpt(unSortArr));
51+
52+
53+
// Time Complexity: O(nSquare)
54+
function sortArrPlayGround(inputArr, type) {
55+
// Step 1: validations checks
56+
type = type.toLowerCase();
57+
const validSortTypes = ['asc', 'desc'];
58+
if (!Array.isArray(inputArr)) {
59+
return 'Input parameter should be array only.';
60+
}
61+
if (!validSortTypes.includes(type)) {
62+
return 'Sort type should be asc or desc only.';
63+
}
64+
65+
// Step 2: start sorting algo
66+
if(type === 'asc') {
67+
for (let i = 0; i < inputArr.length; i++) {
68+
for(let j = i + 1; j < inputArr.length; j++) {
69+
if(inputArr[i] > inputArr[j]) {
70+
const tempEleVal = inputArr[i]
71+
inputArr[i] = inputArr[j];
72+
inputArr[j] = tempEleVal;
73+
}
74+
}
75+
}
76+
}
77+
if(type === 'desc') {
78+
for (let i = 0; i < inputArr.length; i++) {
79+
for(let j = i + 1; j < inputArr.length; j++) {
80+
if(inputArr[i] < inputArr[j]) {
81+
const tempEleVal = inputArr[i]
82+
inputArr[i] = inputArr[j];
83+
inputArr[j] = tempEleVal;
84+
}
85+
}
86+
}
87+
}
88+
return inputArr;
89+
}

0 commit comments

Comments
 (0)