In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian introduces in detail "JavaScript how to find repeating or non-repeating elements in the array", detailed content, clear steps, and proper handling of details. I hope that this article "JavaScript how to find repeating or non-repetitive elements in the array" can help you solve your doubts.
1. Repeat elements or non-repeating elements in the array
Double cycle + slice to find out the duplicate elements. Although you are only required to find the repeating elements, you should be careful to remove the duplicates by the way, otherwise the outer loop will check the repeating elements again; use flag to record the number of repeats and put the elements into the new array only on the first repetition
Function search (arr) {let res = [] let flag = 0 for (let I = 0 position I
< arr.length;i++){ for(let j = i+1;j item[1] >1) .map (item = > item [0]) / / find non-repeating elements That is, the number of occurrences is equal to 1 return [... map.entries ()] .filter (item = > item [1] = = 1) .map (item = > item [0]) / / find out the element return [... map.entries ()] .filter (item = > item [1] = = Math.max (... filter () .map (item = > item [0])} two, array flattening / array dimensionality reduction
A two-dimensional array, taking [[], [{adrug 1}], [], [3pr 4], 5] as an example, gets [{aburel 1}, 3pr 4pr 5] after dimension reduction.
Two-dimensional array: double loop
You need to check whether each element is an array
Function flatten (arr) {const res = [] for (let I = 0 I)
< arr.length; i++){ if(Array.isArray(arr[i])){ for(let j = 0;j < arr[i].length;j++){ res.push(arr[i][j]) } } else { res.push(arr[i]) } } return res}二维数组:循环 + concat concat 本身就可以给数组降维一次 function reduceDiemension(arr){ const res = [] for(let i = 0;i < arr.length;i++){ res = res.concat(arr[i]) } return res}二维数组:reduce + concat 上面的过程本身是一种归并,所以考虑使用 reduce function flatten(arr){ return arr.reduce((acc,cur) =>Acc.concat (cur), [])} 2D array: expand / apply + concat
Convert the array to a parameter list by expanding the original array or using it as the second parameter of apply
Function flatten (arr) {/ / return [] .concat ([], arr) return [] .concat (... arr)}
Multidimensional array, take the following array as an example:
Const arr = [
one,
[
2, [3]
[4,5,6]
[7,8,9]
10,11
]
twelve,
thirteen,
[15,16,17]
]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17]
Multidimensional array: toString + split
Array dimensionality reduction can be seen as removing all parentheses, and the toString method of the array can just do this, and then call the split of the string to convert the string back to the array. However, this approach is limited and requires that the data types of array elements are all the same.
Function flattern_numberArray (arr) {return arr.toString () .split (",") .map (x = > Number)} Multidimensional array: forEach + Recursive function flatten (arr) {const res = [] arr.forEach (item = > {if (Array.isArray (item)) {flatten (item)} else {res.push (item)}) return res} Multidimensional Array: reduce + Recursive
Similarly, the above process is a merge that can be done using reduce. It is important to note that the callback function of reduce must return an array, so stop using push
Function flatten (arr) {return arr.reduce ((acc,cur) = > {if (Array.isArray (cur)) {return [... acc,... flatten (cur)]} else {return [... acc,cur]}, [])} Multidimensional array: while + some
As long as there is an array in the array, use concat to reduce the dimension of the array. This method does not use recursion.
Function flatten (arr) {while (arr.some (item = > Array.isArray (item) {arr = [] .concat (... arr)} return arr} an array of uncertain dimensions: flat
Array dimensionality reduction, directly using the previously mentioned flat is the easiest. Pass parameter 1 by default, which means one dimension reduction; you can pass parameter Infinity to achieve complete dimension reduction, and finally get an one-dimensional array.
Read this, the "JavaScript how to find repeated or non-repetitive elements in the array" article has been introduced, want to master the knowledge of this article still need to practice and use in order to understand, if you want to know more related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.