Let's start with the simple question of how to delete duplicates in an array.
Complex - Use forEach to delete duplicates
First, we create a new empty array, using forEach() Execute the provided function once on each element of the array. Finally check if the value exists in the new array, and if it does not exist, add it.
function removeDuplicates(arr) { const uniqueVals = []; ((value,index) => { if((value) === -1) { (value); } }); return uniqueVals; }
Simple - Use filter to delete duplicates
use filter Methods create a new array containing all elements and test it with the provided function. Basically we just need to iterate over the array and check whether the first position of the current element appears in the array is the same as the current position. Of course, these two positions are different for the repeating elements.
function removeDuplicates(arr) { return ((item, pos) => (item) === pos) }
Simple - Use Set to delete duplicates
ES6 providesSetobject, this makes things easier. Set Only unique values are allowed,So when you pass in an array,It will automatically delete duplicate values。
But if you need an array with unique elements, why not use Set at the beginning?
function removeDuplicates(arr) { return [...new Set(arr)]; }
Next, let's solve the second problem: write a function and pass a set of non-negative integers to the function, with different values, requiring them to be continuous and return the missing number.
For const arr = [4,2,6,8], the output should be
countMissingNumbers(arr)= 3
You can see that 3, 5 and 7 are missing.
Complex - Solved using sort and for loops
To get the smallest and largest numbers, we need to sort them in ascending order using the sort method to achieve this goal, and then loop from the smallest number to the largest number. Each time check whether there is a sequence number that should appear in the array. If it does not exist, add one to the counter.
function countMissingNumbers(arr) { ((a,b) => a-b); let count = 0; const min = arr[0]; const max = arr[-1]; for (i = min; i<max; i++) { if ((i) === -1) { count++; } } return count; }
Simple - Use and solve
There is a simple explanation for this solution: the () function returns the largest number in the array, while the () returns the smallest number in the array.
First, if there are no numbers missing, we can know how many numbers are in the array. Therefore, you can use the following formula maxNumber - minNuber + 1 and use this result to subtract the array length. The difference obtained is the number of missing numbers.
function countMissingNumbers(arr) { return (...arr) - (...arr) + 1 - ; }
The last question is to check if the string is palindrome. The so-called palindrome is a string that reads the same from left to right and from right to left.
Complex - Use for loop checking
The loop of this method starts from the first character of the string and goes half the length of the string. The index of the last character in the string is -1, the index of the second to last character is -2, and so on. So here we check whether the character at the specified index starting from the left is equal to the character at the specified index on the right. If they are not equal, return false.
function checkPalindrome(inputString) { let length = for (let i =0; i<length / 2; i++) { if (inputString[i] !== inputString[length - 1 -i]) { return false } } return true }
Simple - Check with reverse and join
I think this solution is so simple that it doesn't need explanation, because the code itself says it all. We just need to use spread operator Create an array from the string, and thenreverseArray, last use join The method converts it to a string again and compares it with the original string.
function checkPalindrome(string) { return string === [...string].reverse().join(''); }
Keep it simple!
Why make it so complicated when there is an easier way? I hope you can learn some interesting ideas from this article. I wish you a wonderful coding time and try not to complicate simple things in life.
The above is what the editor introduces to you how to make your JavaScript code simple, easy to read, detailed explanation and integration. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!