0% found this document useful (0 votes)
12 views6 pages

Array Q29

The document presents various programming problems related to finding unique elements in arrays and strings, including finding an element that appears only once in a duplicate array, the second most frequent character in a string, and the most frequent word in a sentence. It provides example inputs and outputs, along with JavaScript solutions for each problem. Additionally, it offers information on purchasing educational PDF guides and joining coding classes.

Uploaded by

manickvel60
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Array Q29

The document presents various programming problems related to finding unique elements in arrays and strings, including finding an element that appears only once in a duplicate array, the second most frequent character in a string, and the most frequent word in a sentence. It provides example inputs and outputs, along with JavaScript solutions for each problem. Additionally, it offers information on purchasing educational PDF guides and joining coding classes.

Uploaded by

manickvel60
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Reel 29:

Find the Element That Appears Only Once in a Duplicate Array:

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

// Count occurrences of each number


for (let num of arr) {
frequency[num] = (frequency[num] || 0) + 1;
}

// Find the first number that appears only once


for (let num of arr) {
if (frequency[num] === 1) {
return num;
}
}

return null; // If no unique number is found


}

// 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:

1. Create an empty object frequency to store the count of each number.


2. Loop through the array and update the count for each number.
3. Loop through the array again and return the first number that has a count of 1.
4. If all numbers repeat, return null.

Find the Second Most Frequent Character in a String

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 = {};

// Count occurrences of each character


for (let char of str) {
frequency[char] = (frequency[char] || 0) + 1;
}

// Convert object to an array and sort by frequency in descending order


let sortedChars = Object.entries(frequency).sort((a, b) => b[1] - a[1]);

// Return second most frequent character if it exists


return sortedChars.length > 1 ? sortedChars[1][0] : null;
}

// Test cases
console.log(secondMostFrequentChar("javascript")); // Output: "v"
console.log(secondMostFrequentChar("apple")); // Output: "a"
console.log(secondMostFrequentChar("aaa")); // Output: null

Explanation:

1. Use an object (frequency) to count occurrences of each character.


2. Convert the object into an array and sort it in descending order by frequency.
3. Return the second most frequent character if it exists; otherwise, return null.

Find the Element That Appears Only Once in a Triplet Array

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

(Explanation: 5 and 1 appear three times, but 7 appears only once.)

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;
}

// Find the number that appears only once


for (let num in frequency) {
if (frequency[num] === 1) {
return Number(num); // Convert string key to number
}
}

return null; // If no unique number is found


}

// 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:

1. Create an empty object frequency to store the count of each number.


2. Loop through the array and update the count for each number.
3. Loop through the object to find the number that appears exactly once.
4. If all numbers appear three times, return null.

Find the Most Frequent Word in a Sentence

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 = {};

// Count occurrences of each character


for (let char of str) {
frequency[char] = (frequency[char] || 0) + 1;
}

// Convert object to an array and sort by frequency in descending order


let sortedChars = Object.entries(frequency).sort((a, b) => b[1] - a[1]);

// Return second most frequent character if it exists


return sortedChars.length > 1 ? sortedChars[1][0] : null;
}

// Test cases
console.log(secondMostFrequentChar("javascript")); // Output: "v"
console.log(secondMostFrequentChar("apple")); // Output: "a"
console.log(secondMostFrequentChar("aaa")); // Output: null

Explanation:

1. Use an object (frequency) to count occurrences of each character.


2. Convert the object into an array and sort it in descending order by frequency.
3. Return the second most frequent character if it exists; otherwise, return null.

For More Interview Questions:

Get Your PDF Files Instantly:

Structured learning made simple! Choose from our affordable guides:

1. JavaScript & DSA with 700+ Questions Interview ➔ ₹150/- (Price may increase
with weekly updates!)

100+ JavaScript theory questions to master core concepts

60+ output-based questions to test JavaScript behavior


50+ coding tasks with detailed explanations

550+ LeetCode & Hackathon coding challenges covering interview Q’s from
FAANG & top product-based companies
Weekly updates with coding questions

2. React Coding Interview Questions & Answers ➔ ₹50/-


→ Covers essential React concepts and problem-solving techniques.
3. Backend Development PDF ➔ ₹60/-
→ Focused on Node.js, Express.js, MongoDB, and related technologies.

How to Get It?

• Message us on WhatsApp: 9398123134


• Pay the respective amount, and receive your PDF instantly!

Instant replies are available between 8:00 AM to 10:00 PM IST.

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

Join Our Online and Offline Classes

Upskill with confidence—our courses come with a


100% PLACEMENT GUARANTEE!

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

Call us at 9398123134 to get started today!

You might also like