Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d243569

Browse files
committedDec 9, 2021
arrays
1 parent dd71043 commit d243569

File tree

8 files changed

+208
-0
lines changed

8 files changed

+208
-0
lines changed
 

‎averagePair.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function averagePair(arr, num) {
2+
let start = 0;
3+
let end = arr.length - 1;
4+
while (start < end) {
5+
let avg = (arr[start] + arr[end]) / 2;
6+
if (avg === num) return true;
7+
else if (avg < num) start++;
8+
else end--;
9+
}
10+
return false;
11+
}
12+
13+
console.log(averagePair([2,6,7,8,1,5],4))

‎countUniqueValues.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
let countUniqueValues = (arr) => {
2+
// arr is sorted
3+
let count = 0
4+
5+
if (arr.length == 0) {
6+
// return 0 if array is empty
7+
count = 0;
8+
} else if (arr.length == 1){
9+
// return 1 if there's only 1 item in the array
10+
count = 1;
11+
} else {
12+
count = 1
13+
let left = 0,
14+
right = arr.length - 1;
15+
16+
while (arr[left] < arr[right]) {
17+
if (arr[left] < arr[left + 1]) count++;
18+
left++;
19+
}
20+
}
21+
22+
console.log(count)
23+
return count
24+
}
25+
26+
countUniqueValues([-2,-2,0])

‎duplicateChecker.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// using frequency counter
2+
function areThereDuplicates() {
3+
let collection = {};
4+
for (let val in arguments) {
5+
collection[arguments[val]] = (collection[arguments[val]] || 0) + 1;
6+
}
7+
console.log(collection)
8+
for (let key in collection) {
9+
if (collection[key] > 1) return true;
10+
}
11+
return false;
12+
}
13+
14+
console.log("using frequency counter: "+areThereDuplicates(1,5,6,6,3,4,9));
15+
16+
// using multiple pointers
17+
function areThereDuplicates(...args) {
18+
// Two pointers
19+
args.sort((a, b) => a > b);
20+
let start = 0;
21+
let next = 1;
22+
while (next < args.length) {
23+
if (args[start] === args[next]) {
24+
return true;
25+
}
26+
start++;
27+
next++;
28+
}
29+
return false;
30+
}
31+
32+
console.log(
33+
"using multiple pointers: " + areThereDuplicates(1, 5, 6, 3, 4, 9)
34+
);
35+
36+
// using one liner solution
37+
function areThereDuplicates() {
38+
return new Set(arguments).size !== arguments.length;
39+
}
40+
console.log(
41+
"using one liner solution: " + areThereDuplicates(1, 5, 6, 6, 3, 4, 9)
42+
);

‎longestSubString.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
// Write a function called findLongestSubstring, which accepts a string
3+
// and returns the length of the longest substring with all distinct characters.
4+
5+
function findLongestSubstring(str) {
6+
let longest = 0;
7+
let seen = {};
8+
let start = 0;
9+
10+
for (let i = 0; i < str.length; i++) {
11+
let char = str[i];
12+
if (seen[char]) {
13+
start = Math.max(start, seen[char]);
14+
}
15+
// index - beginning of substring + 1 (to include current in count)
16+
longest = Math.max(longest, i - start + 1);
17+
// store the index of the next char so as to not double count
18+
seen[char] = i + 1;
19+
}
20+
return longest;
21+
}
22+
23+
console.log(findLongestSubstring("leech"));

‎maxSubArray.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let maxSubArray = (arr, n) => {
2+
let maxSum = 0
3+
let tempSum = 0
4+
if(arr.length < n) return null
5+
for(let i=0;i<n;i++){
6+
maxSum += arr[i]
7+
}
8+
tempSum = maxSum
9+
for(let i=n;i<arr.length;i++){
10+
tempSum += arr[i] - arr[i - n];
11+
maxSum=Math.max(maxSum,tempSum)
12+
}
13+
console.log(maxSum)
14+
return maxSum
15+
}
16+
17+
maxSubArray([2,4,7,1,4,5,9,1],1);

‎minSubArrLen.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Write a function called minSubArrayLen which accepts two parameters -
2+
// an array of positive integers and a positive integer.
3+
4+
// This function should return the minimal length of a contiguous subarray of which the sum is greater
5+
// than or equal to the integer passed to the function. If there isn't one, return 0 instead.Examples:
6+
7+
function minSubArrayLen(nums, sum) {
8+
let total = 0;
9+
let start = 0;
10+
let end = 0;
11+
let minLen = Infinity;
12+
13+
while (start < nums.length) {
14+
// if current window doesn't add up to the given sum then
15+
// move the window to right
16+
if (total < sum && end < nums.length) {
17+
total += nums[end];
18+
end++;
19+
}
20+
// if current window adds up to at least the sum given then
21+
// we can shrink the window
22+
else if (total >= sum) {
23+
minLen = Math.min(minLen, end - start);
24+
total -= nums[start];
25+
start++;
26+
}
27+
// current total less than required total but we reach the end, need this or else we'll be in an infinite loop
28+
else {
29+
break;
30+
}
31+
}
32+
33+
return minLen === Infinity ? 0 : minLen;
34+
}
35+
36+
console.log(minSubArrayLen([2, 3, 1, 2, 4, 3], 7));
37+
console.log(minSubArrayLen([2, 1, 6, 5, 4], 9));

‎sameFrequency.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function sameFrequency(num1, num2) {
2+
let strNum1 = num1.toString();
3+
let strNum2 = num2.toString();
4+
if (strNum1.length !== strNum2.length) return false;
5+
6+
let countNum1 = {};
7+
let countNum2 = {};
8+
9+
for (let i = 0; i < strNum1.length; i++) {
10+
countNum1[strNum1[i]] = (countNum1[strNum1[i]] || 0) + 1;
11+
}
12+
13+
for (let j = 0; j < strNum1.length; j++) {
14+
countNum2[strNum2[j]] = (countNum2[strNum2[j]] || 0) + 1;
15+
}
16+
17+
for (let key in countNum1) {
18+
if (countNum1[key] !== countNum2[key]) return false;
19+
}
20+
21+
return true;
22+
}
23+
24+
console.log(sameFrequency("wrong", "grown"));
25+
console.log(sameFrequency("grab", "crab"));

‎subSequence.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
// Iterative
3+
function isSubsequence(str1, str2) {
4+
var i = 0;
5+
var j = 0;
6+
if (!str1) return true;
7+
while (j < str2.length) {
8+
if (str2[j] === str1[i]) i++;
9+
if (i === str1.length) return true;
10+
j++;
11+
}
12+
return false;
13+
}
14+
15+
console.log("Iterative: "+isSubsequence("str1", "str2"));
16+
17+
// Recursive but not O(1) Space
18+
function isSubsequence(str1, str2) {
19+
if (str1.length === 0) return true;
20+
if (str2.length === 0) return false;
21+
if (str2[0] === str1[0]) return isSubsequence(str1.slice(1), str2.slice(1));
22+
return isSubsequence(str1, str2.slice(1));
23+
}
24+
25+
console.log("Recursive: "+isSubsequence("str1", "str2"));

0 commit comments

Comments
 (0)
Please sign in to comment.