Introduction
Having a basic understanding of programming languages is essential for solving algorithm problems, which are a crucial element of coding tests. In particular, JavaScript is a language that is essential for web development and frontend fields, and many companies are presenting problems using JavaScript. In this course, we will learn the applicability of JavaScript through the problem of ‘Good Numbers’ and explain the algorithm problem-solving process in detail.
Problem Description
The ‘Good Numbers’ problem is about finding numbers that satisfy certain conditions among the given numbers. The specifics of this problem are as follows:
Problem: Given an array of positive integers, write a function that removes duplicates from the array and prints all numbers less than 10. Additionally, calculate the average of the remaining numbers up to one decimal place and return it.
Input and Output Format
Input: An array of positive integers arr ([1, 2, 2, 3, 10, 5, 7, 15])
Output:
1. List of numbers less than 10 after removing duplicates
2. Average of the remaining numbers (up to one decimal place)
Example
Input: [1, 2, 2, 3, 10, 5, 7, 15] Output: Numbers after removing duplicates (less than 10): [1, 2, 3, 5, 7] Average: 3.6
Problem-Solving Approach
To solve the problem, we need to follow the steps of removing duplicates from the array and calculating the average of the filtered array. We will look at how to perform these tasks step by step using JavaScript.
Step 1: Remove Duplicates
We can use the Set object to remove duplicate elements from the array. The Set object does not allow duplicates automatically, so we can easily obtain the desired result using this object.
const arr = [1, 2, 2, 3, 10, 5, 7, 15];
const uniqueArr = [...new Set(arr)];
console.log(uniqueArr); // [1, 2, 3, 10, 5, 7, 15]
Step 2: Filtering
Now, we will perform the task of filtering numbers less than 10 from the array with duplicates removed. We can use JavaScript’s filter() method to extract only the elements that meet the condition.
const filteredArr = uniqueArr.filter(num => num < 10);
console.log(filteredArr); // [1, 2, 3, 5, 7]
Step 3: Calculate Average
To calculate the average of the filtered array, we can use the reduce() method. We can get the average by summing all the elements of the array and dividing the total by the number of elements.
const average = filteredArr.reduce((acc, num) => acc + num, 0) / filteredArr.length;
console.log(average.toFixed(1)); // 3.6
Complete Code
Now, we will integrate all the described processes into a single function to write the final code.
function goodNumbers(arr) {
const uniqueArr = [...new Set(arr)];
const filteredArr = uniqueArr.filter(num => num < 10);
const average = filteredArr.reduce((acc, num) => acc + num, 0) / filteredArr.length;
return {
filteredNumbers: filteredArr,
average: average.toFixed(1)
};
}
const inputArr = [1, 2, 2, 3, 10, 5, 7, 15];
const result = goodNumbers(inputArr);
console.log(result);
Result
When the code above is executed, the following result can be obtained.
{
filteredNumbers: [1, 2, 3, 5, 7],
average: "3.6"
}
Optimization and Considerations
The above problem-solving method works well in practical applications. However, when dealing with large datasets, additional considerations regarding performance may be necessary. For example, while using a Set is a convenient way to extract unique values, it may lead to increased memory usage if the size of the array is very large. In such cases, several methods can be considered to improve the algorithm’s performance:
- Explore methods to remove duplicates and filter at the same time.
- Improve performance based on the choice of data structure.
Conclusion
In this course, we explored the process of solving the ‘Good Numbers’ problem using JavaScript. By removing duplicates, filtering numbers according to conditions, and calculating averages, we were able to enhance our basic algorithm problem-solving skills. It is important to practice solving such problems frequently while preparing for coding tests. I hope you gain a deeper understanding by utilizing the various features of JavaScript.
Did You Find This Helpful?
If this course has helped you prepare for JavaScript coding tests, try challenging other algorithm problems as well. Feel free to leave any difficulties or questions in the comments. I hope you have the opportunity to share your learning journey and grow together.