English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes the method of array deduplication implemented by JS. Shared for everyone to refer to, as follows:
Running effect diagram is as follows:
Complete instance code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Test</title> <script type="text/javascript" language="javascript" > Array.prototype.distinct = function() { var $ = this; var o1 = {}; //Store non-duplicate values var o2 = {}; //Store duplicate values var o3 = []; //Store duplicate values var o; //Array single variable for (var i = 0; o = $[i]; i++{ if (o in o1{ if (!(o in o2)) o2[o] = o; delete $[i]; }else{}} o1[o] = o; } } $.length = 0; //Clear the original array for(o in o1{ $.push(o); } for(o in o2{ o3.push(o); } return o3; } var a = [2,2,2,3,3,3,4,4,5,6,7,7]; console.log("Original array:") + a); //2,2,2,3,3,3,4,4,5,6,7,7 console.log("The duplicated elements are:") + a.distinct()); //2,3,4,7 console.log("The sorted array is:") + a); //2,3,4,5,6,7 console.log("The length after sorting is:") + a.length) //6 </script> </head> <body> </body> </html>
PS: Here are several duplicate removal tools provided for everyone's reference and use:
Online Duplicate Item Removal Tool:
http://tools.jb51.net/code/quchong
Online Text Duplicate Removal Tool:
http://tools.jb51.net/aideddesign/txt_quchong
Readers who are interested in more content related to JavaScript can check the special topics of this site: 'Summary of JavaScript Array Operation Skills', 'Summary of JavaScript Sorting Algorithms', 'Summary of JavaScript Mathematical Operation Usage', 'Summary of JavaScript Data Structure and Algorithm Skills', 'Summary of JavaScript Traversal Algorithms and Skills', 'Summary of JavaScript Search Algorithm Skills', and 'Summary of JavaScript Error and Debugging Skills'.
Hoping the content described in this article will be helpful to everyone's JavaScript program design.
Statement: The content of this article is from the network, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not bear relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)