This time I will bring you the fastest algorithm for deduplication in JSarray, what are the notes for deduplication in JS array, the following is a practical case, let’s take a look take a look.
In JS, we often encounter the need to remove duplicate data in arrays. Here we introduce four algorithms to achieve the function of deduplication in JS arrays.
1. The fastest algorithm:ObjectKey-value pair method
Implementation idea: Create a new js object and a new array. When traversing the incoming array, determine whether the value is the key of the js object. If not, add the key to the object and put the new key in it. array.
//注意点: 判断 是否为js对象键时,会自动对传入的键执行“toString()”,不同的键可能会被误认为一样;例如: a[1]、a["1"] 。解决上述问题还是得调用“indexOf”。 //速度最快, 占空间最多(空间换时间) function unique(array){ var n = {}, r = [], len = array.length, val, type; for (var i = 0; i <p style="text-align: left;"> operation result:</p><p style="text-align: left;"> <img src="http://img./attachment/art/151545/1466d89b17.jpg?2017101711014" alt="The fastest algorithm to remove duplicates from JS arrays"></p><p style="max-width:90%"> <span style="font-size:medium;"><strong>2. The most ingenious algorithm: optimization<a href="http://www.php.cn/code/6716.html" target="_blank">Traverse the array</a>method</strong></span></p><p style="text-align: left;"> <strong>Implementation idea: </strong>Get the rightmost value without duplication and put it into a new array. (When duplicate values are detected, the current loop is terminated and the next round of judgment of the top-level loop is entered) </p><pre class="brush:php;toolbar:false">function unique1(array){ var r = []; for(var i = 0, l = array.length; i<l var console.log><p style="text-align: left;"> operation result:</p> <p style="text-align: left;"> <img src="http://img./attachment/art/151545/1049c0807f.jpg?2017101711520" alt="The fastest algorithm to remove duplicates from JS arrays"></p> <p style="text-align: left;"> <span style="font-size:medium;"><strong>3. Algorithm: Sorted Adjacent Removal Method</strong></span></p> <p style="text-align: left;"> <strong> Implementation idea: </strong> Sort the incoming <a href="http://www.php.cn/code/54.html" target="_blank"> array </a>. After sorting, the same values are adjacent, and then when traversing, only add values that are not duplicates of the previous value to the new array. </p> <pre class="brush:php;toolbar:false">//将相同的值相邻,然后遍历去除重复值 function unique2(array){ array.sort(); var re=[array[0]]; for(var i = 1; i <p style="text-align: left;"> operation result:</p><p style="text-align: left;"> <img src="http://img./attachment/art/151545/d37e2cf87e.jpg?2017101711734" alt="The fastest algorithm to remove duplicates from JS arrays"></p><p style="max-width:90%"> <span style="font-size:medium;"><strong>4. Algorithm: Array subscript judgment method</strong></span></p><p style="text-align: left;"> <strong>Implementation idea: </strong>If the first occurrence of the i-th item in the current array is not i, then it means that the i-th item is repeated and ignored. Otherwise, store the result array </p><pre class="brush:php;toolbar:false">function unique3(array){ var n = [array[0]]; //结果数组 //从第二项开始遍历 for(var i = 1; i <p> I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website! </p><p>Recommended reading:</p><p><a href="http://www.php.cn/js-tutorial-392720.html" target="_blank">JS method to remove duplicate items in an array</a><br></p><p><a href="http://www.php.cn/js-tutorial-392713.html" target="_blank">How to use bootstrap responsive navigation bar template </a><br></p><p><a href="http://www.php.cn/js-tutorial-392700.html" target="_blank">vue.js method of operating array data</a><br></p><p style="text-align: left;"> </p><!--content end-->
The above is the detailed content of The fastest algorithm to remove duplicates from JS arrays. For more information, please follow other related articles on the PHP Chinese website!