Repository navigation
Expand file tree
/
Copy pathEG-13_Sprint-2_PS.js
More file actions
100 lines (90 loc) · 2.86 KB
/
Copy pathEG-13_Sprint-2_PS.js
File metadata and controls
100 lines (90 loc) · 2.86 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 01. Reverse a String
function reverseString(str) {
return str.split('').reverse().join('');
}
console.log("01. Reverse String:", reverseString("hello"));
// 02. Find Maximum
function findMax(nums) {
return Math.max(...nums);
}
console.log("02. Find Max:", findMax([3, 7, 2, 9, 5]));
// 03. Check for Palindrome
function isPalindrome(str) {
return str === str.split('').reverse().join('');
}
console.log("03. Is Palindrome:", isPalindrome("racecar"));
// 04. Sum Array Elements
function sumArray(nums) {
return nums.reduce((total, num) => total + num, 0);
}
console.log("04. Sum Array:", sumArray([10, 20, 30, 40]));
// 05. Count Vowels
function countVowels(str) {
const matches = str.match(/[aeiou]/gi);
return matches ? matches.length : 0;
}
console.log("05. Count Vowels:", countVowels("javascript"));
// 06. Two Sum
function twoSum(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i];
}
map.set(nums[i], i);
}
return [];
}
console.log("06. Two Sum:", JSON.stringify(twoSum([2, 7, 11, 15], 9)));
// 07. Flatten a Nested Array
function flattenArray(arr) {
return arr.flat(Infinity);
}
console.log("07. Flatten Array:", JSON.stringify(flattenArray([1, [2, [3, 4], 5]])));
// 08. Group Anagrams
function groupAnagrams(strs) {
const map = new Map();
for (const str of strs) {
const sortedKey = str.split('').sort().join('');
if (!map.has(sortedKey)) {
map.set(sortedKey, []);
}
map.get(sortedKey).push(str);
}
return Array.from(map.values());
}
console.log("08. Group Anagrams:", JSON.stringify(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])));
// 09. Longest Substring Without Repeating Characters
function lengthOfLongestSubstring(s) {
let maxLength = 0;
let start = 0;
const charMap = new Map();
for (let end = 0; end < s.length; end++) {
const currentChar = s[end];
if (charMap.has(currentChar) && charMap.get(currentChar) >= start) {
start = charMap.get(currentChar) + 1;
}
charMap.set(currentChar, end);
maxLength = Math.max(maxLength, end - start + 1);
}
return maxLength;
}
console.log("09. Longest Substring Length:", lengthOfLongestSubstring("abcabcbb"));
// 10. Deep Clone an Object
function deepClone(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(item => deepClone(item));
}
const clone = {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
clone[key] = deepClone(obj[key]);
}
}
return clone;
}
console.log("10. Deep Clone:", JSON.stringify(deepClone({ a: 1, b: { c: 2 } })));