What is the deduplication method of Java array
This article mainly explains "what is the method of removing repetition of Java array". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the train of thought of Xiaobian to study and learn "what is the method of removing repetition of Java array".
The scene of array deduplication
Filter out the repeated values in the array var arr = [1, null,null, NaN, NaN, 0, 0,'a moment,'a moment, {}, {}].
1 、 ES6-set
Using set in ES6 is the easiest way to remove weight.
Var arr=] / / convert array to set var set=new Set (arr) / / then convert set to array console.log (Array.from (set)) 2, use Map data structure to remove duplicates
Create an empty Map data structure, iterate through the array that needs to be deduplicated, and store each element of the array in Map as key. Since the same key value will not appear in Map, the final result is the de-duplicated result.
Function shuzu (arr) {let map = new Map (); let array = new Array (); / / the array is used to return the result for (let I = 0; I
< arr.length; i++) { if(map .has(arr[i])) { // 如果有该key值 map .set(arr[i], true); } else { map .set(arr[i], false); // 如果没有该key值 array .push(arr[i]); } } return array ;}3、 嵌套循环+splicefunction shuzu(arr){for(var i = 0 ; i < arr.length; i++){for( var j = i + 1; j < arr.length; j++){if( arr[i] === arr[j] ){arr.splice(j,1);}}}return arr;}4、 forEach + indexOffunction shuzu(arr){var res = [];arr.forEach((val,index)=>{if (res.indexOf (val) =-1) {res.push (val);}}); return res;} Thank you for your reading, this is the content of "what is the deduplication method of Java array". After the study of this article, I believe you have a deeper understanding of what the deduplication method of Java array is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!