Skip to content

Commit 6628ed5

Browse files
author
Connor Leech
committed
add max char and int reverse
1 parent f17ef16 commit 6628ed5

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The Coding Interview Bootcamp: Algorithms + Data Structures
2+
3+
4+
Udemy course link: [The Coding Interview Bootcamp: Algorithms + Data Structures](https://www.udemy.com/coding-interview-bootcamp-algorithms-and-data-structure)
5+
6+
[Source code](https://github.com/StephenGrider/algocasts) for the lessons.
7+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
// reverseInteger(500) === 5
3+
// reverseInteger(981) === 189
4+
// reverseInteger(-15) === -51
5+
// reverseInteger(-90) === -9
6+
7+
function reverseInteger(integer) {
8+
let isNegative = false;
9+
10+
if(integer < 0) isNegative = true;
11+
12+
const reversed = String(integer).split('').reverse().join('');
13+
14+
return (isNegative) ? parseInt(`-${reversed}`) : parseInt(`${reversed}`);
15+
}
16+
17+
18+
19+
// You can use Math.sign to determine the +/- of a number
20+
// (could use reverse function as above)
21+
22+
function reverseIntegerSolution(integer){
23+
const reversed = integer.toString().split('').reduce((current, accum)=>{
24+
return accum + current;
25+
}, '');
26+
27+
return Math.sign(integer) * parseInt(reversed);
28+
}
29+
30+
console.log(reverseIntegerSolution(-15));
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// --- Directions
2+
// Given a string, return the character that is most
3+
// commonly used in the string.
4+
// --- Examples
5+
// maxChar("abcccccccd") === "c"
6+
// maxChar("apple 1231111") === "1"
7+
8+
function maxChar(str) {
9+
let map = {};
10+
let maxCount = 0;
11+
let letter;
12+
13+
str.split('').forEach((letter)=>{
14+
map[letter] = (map[letter]) ? map[letter] + 1 : 1;
15+
});
16+
17+
for(key in map){
18+
if(map[key] > maxCount){
19+
maxCount = map[key];
20+
letter = key;
21+
}
22+
23+
}
24+
25+
return letter;
26+
}
27+
28+
29+
30+
function maxCharSolution(str){
31+
const charMap = {};
32+
let max = 0;
33+
let maxChar = '';
34+
35+
for(let char of str) {
36+
if(charMap[char]){
37+
charMap[char]++;
38+
} else {
39+
charMap[char] = 1;
40+
}
41+
}
42+
43+
for(char in charMap){
44+
if(charMap[char] > max){
45+
max = charMap[char];
46+
maxChar = char;
47+
}
48+
}
49+
50+
return maxChar;
51+
}
52+
53+
console.log(maxCharSolution("abcccccccd"));
54+
55+
56+
/******************
57+
Most common string questions:
58+
59+
- What is the most common character in this string?
60+
- Does string A have the same characters as string B? (anagram)
61+
- Does the string have any repeating characters in it?
62+
******************/

0 commit comments

Comments
 (0)