-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblems.js
46 lines (43 loc) · 1.23 KB
/
problems.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 01 -- count the character
// time complexity Big O(n) or linear
// space complexity O(n)
function countCharacter(string) {
// creating a new object
const hashMap = {};
// normalized the object
const normalized = string.toLowerCase();
// looping the object
for (let i = 0; i < normalized.length; i++) {
const char = normalized[i];
// ignoring the space
if (char === " ") continue;
// if the character is existing in object
if (char in hashMap) {
hashMap[char] = hashMap[char] + 1;
} else {
hashMap[char] = 1;
}
}
return hashMap;
}
const letter = countCharacter("Hello world");
console.log(letter);
// 02 -- is element exists
// time complexity Big O(n*n)
// space complexity O(1) - because output dose not depends on input
function isExists(arr1, arr2) {
// initial state to return a single result out of loop
let state = false;
// loop the 1st array
for (elm of arr1) {
// looping 2nd array & check if element of 1st array is included in 2nd array
if (arr2.includes(elm)) {
state = true;
// if the condition is true no need to check next condition
break;
} else state = false;
}
return state;
ex;
}
console.log(isExists(["a", "b", "c"], [1, "b", 3]));