String Q28
String Q28
Problem: You are given a string. Write a program to find the most frequently occurring
character in the string. If multiple characters have the same highest frequency, return the first
one encountered.
Input: "javascript"
Output: "a"
(Explanation: The letter "a" appears twice, which is the highest frequency.)
Input: "apple"
Output: "p"
(Explanation: The letter "p" appears twice, which is the most frequent
character.)
Input: "hello world"
Output: "l"
(Explanation: The letter "l" appears three times, which is the highest
frequency.)
Input: "abcd"
Output: "a"
(Explanation: All characters appear only once, so we return the first
character encountered.)
Solution:
function mostFrequentCharacter(str) {
let frequency = {}; // Object to store character counts
let maxChar = ""; // Stores the character with the highest frequency
let maxCount = 0; // Stores the highest frequency count
return maxChar;
}
// Test cases
console.log(mostFrequentCharacter("javascript")); // Output: "a"
console.log(mostFrequentCharacter("apple")); // Output: "p"
console.log(mostFrequentCharacter("hello world")); // Output: "l"
console.log(mostFrequentCharacter("abcd")); // Output: "a"
Explanation:
O(n) → We iterate through the string once to count characters and once to find the max.
Problem: You are given a string. Write a program to find the first non-repeating character in
the string. If all characters repeat, return null.
Examples:
Input:
"javascript"
Output:
"j"
(Explanation: "j" appears only once, and it's the first unique character.)
Input:
"apple"
Output:
"a"
(Explanation: "a" appears once before other unique characters.)
Input:
"aabbcc"`
Output:
null
(Explanation: All characters repeat, so we return null.)
Solution:
function firstNonRepeatingCharacter(str) {
let frequency = {};
// Test cases
console.log(firstNonRepeatingCharacter("javascript")); // Output: "j"
console.log(firstNonRepeatingCharacter("apple")); // Output: "a"
console.log(firstNonRepeatingCharacter("aabbcc")); // Output: null
Explanation:
Problem: You are given two strings. Write a program to check if they are anagrams (contain
the same letters in a different order).
Examples:
Input:
"listen", "silent"
Output:
true
(Explanation: Both words contain the same letters, rearranged.)
Input:
"hello", "world"
Output:
false
(Explanation: Different letters, so not anagrams.)
Solution:
// Test cases
console.log(areAnagrams("listen", "silent")); // Output: true
console.log(areAnagrams("hello", "world")); // Output: false
console.log(areAnagrams("triangle", "integral")); // Output: true
Explanation:
Problem: You are given a sentence as a string. Write a program to find the longest word in
the sentence.
Examples:
Input:
"The quick brown fox jumps"
Output:
"jumps"
(Explanation: "jumps" is the longest word with 5 letters.)
Input:
"I love JavaScript"
Output:
"JavaScript"
(Explanation: "JavaScript" is the longest word with 10 letters.)
Solution:
function longestWord(sentence) {
let words = sentence.split(" ");
let longest = "";
return longest;
}
// Test cases
console.log(longestWord("The quick brown fox jumps")); // Output: "jumps"
console.log(longestWord("I love JavaScript")); // Output: "JavaScript"
console.log(longestWord("Coding is fun")); // Output: "Coding"
Explanation:
1. Split the sentence into an array of words.
2. Iterate through each word, keeping track of the longest one.
3. Return the longest word found.
Problem: Write a program to check if a given string contains only unique characters.
Examples:
Input:
"abcdef"
Output:
true
(Explanation: All characters are unique.)
Input:
"hello"
Output:
false
(Explanation: "l" appears twice.)
Input:
"world"
Output:
true
(Explanation: All characters are unique.)
Solution:
function hasUniqueCharacters(str) {
let seen = new Set();
return true;
}
// Test cases
console.log(hasUniqueCharacters("abcdef")); // Output: true
console.log(hasUniqueCharacters("hello")); // Output: false
console.log(hasUniqueCharacters("world")); // Output: true
Explanation:
Coming Soon (02.02.2025 @6:30 pm): 500+ Best Questions from LeetCode and
HackerRank
Carefully selected questions frequently asked by top companies to help you ace your
interviews!
How to Get It?
1. Frontend Technologies
Learn ReactJS, Angular, and modern UI/UX design.
2. Full Stack Development
Master MERN stack, Java, Python, or .NET technologies.
3. Data Science and Analytics
Gain skills in data visualization, modeling, and analysis.
4. Artificial Intelligence and Machine Learning
Explore AI tools, concepts, and practical implementations.
5. Advanced Topics
o Cloud Computing: AWS, Azure
o Cybersecurity: Ethical hacking, penetration testing
How to Enroll?