Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the deduplication method of Java array

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report