Array Q29
Array Q29
1. Problem:
You are given an array of integers where every number appears twice, except for one number
that appears only once. Write a program to find that unique number. If no such number
exists, return null.
Examples:
Input:
[4, 3, 2, 4, 1, 3, 2];
Output:
1;
(Explanation: Every number appears twice, except 1, which appears only once.)
Input:
[7, 8, 9, 8, 7];
Output:
9;
(Explanation: 7 and 8 appear twice, but 9 appears only once.)
Input:
[5, 5, 6, 6, 7, 7];
Output:
Null
(Explanation: Every number appears twice, so there's no unique number.)
Solution:
function findUniqueNumber(arr) {
let frequency = {}; // Object to store number counts
// Test cases
console.log(findUniqueNumber([4, 3, 2, 4, 1, 3, 2])); // Output: 1
console.log(findUniqueNumber([7, 8, 9, 8, 7])); // Output: 9
console.log(findUniqueNumber([5, 5, 6, 6, 7, 7])); // Output: null
Explanation:
2. Problem:
You are given a string. Write a program to find the second most frequent character in the
string. If there is no second most frequent character, return null.
Examples:
Input:
"javascript";
Output:
"v";
Solution:
function secondMostFrequentChar(str) {
let frequency = {};
// Test cases
console.log(secondMostFrequentChar("javascript")); // Output: "v"
console.log(secondMostFrequentChar("apple")); // Output: "a"
console.log(secondMostFrequentChar("aaa")); // Output: null
Explanation:
3. Problem:
You are given an array where every number appears exactly three times, except for one
number that appears only once. Write a program to find that unique number.
Examples:
Input:
[2, 2, 3, 2];
Output: 3
(Explanation: The number 2 appears three times, but 3 appears only once.)
Input:
[5, 1, 5, 1, 5, 1, 7];
Output:
7
Input:
[8, 8, 8];
Output:
Null
function findUniqueTriplet(arr) {
let frequency = {}; // Object to store number counts
// Count occurrences of each number
for (let num of arr) {
frequency[num] = (frequency[num] || 0) + 1;
}
// Test cases
console.log(findUniqueTriplet([2, 2, 3, 2])); // Output: 3
console.log(findUniqueTriplet([5, 1, 5, 1, 5, 1, 7])); // Output: 7
console.log(findUniqueTriplet([8, 8, 8])); // Output: null
Explanation:
4. Problem: You are given a sentence. Write a program to find the most frequently
occurring word in the sentence. If multiple words have the same frequency, return the first
one encountered.
Examples:
Input:
"the cat and the dog";
Output:
"the";
(Explanation: "the" appears twice, which is the highest frequency.)
Input:
"hello world hello everyone";
Output:
"hello";
Solution:
function secondMostFrequentChar(str) {
let frequency = {};
// Test cases
console.log(secondMostFrequentChar("javascript")); // Output: "v"
console.log(secondMostFrequentChar("apple")); // Output: "a"
console.log(secondMostFrequentChar("aaa")); // Output: null
Explanation:
1. JavaScript & DSA with 700+ Questions Interview ➔ ₹150/- (Price may increase
with weekly updates!)
550+ LeetCode & Hackathon coding challenges covering interview Q’s from
FAANG & top product-based companies
Weekly updates with coding questions
If you do not receive a reply within 10 minutes on WhatsApp or if you need a PDF
outside of these hours, please visit:
https://topmate.io/smaruthihema100_hema
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
6. Power BI