search
HomeWeb Front-endJS TutorialVarious methods to achieve array deduplication in javascript_javascript skills

No further nonsense, let’s get straight to the practical stuff!

Let’s first talk about the requirements of this example: write a method to achieve deduplication of the array. (Requirements: Execute method, pass an array, return the new array after deduplication, the original array remains unchanged, only one layer of loops can be used during the implementation process, double-layer nested loops can also be written, for reference only);

First, let me explain to beginners what array deduplication is (skip for veterans): it means to remove duplicate elements in the array, for example, var arr = [3,2,4,2,1,2]; The new array obtained by deduplicating the array is [3,2,4,1], which is such a function.

The implementation method is relatively simple, and there are many ways to implement it. Many experts have also written related articles. The purpose of writing this blog is firstly to serve as a reminder, and secondly to give beginners a better understanding of it. The principle of implementation, okay, let’s look at the first implementation method:

The first method is to remove duplicates by traversing the new array

var arr = [1,'b','b',4,3,3,4,5,1];
     
    //第一种
    Array.prototype.unique1 = function(){
      var arr1 = []; //定义一个新数组
      for(var i=0;i<this.length;i++){
        if(arr1.indexOf(this[i]) == -1){//判断目标数组中在原数组里是否存在
          arr1.push(this[i]);
        } 
      } 
      return arr1;
    }
    console.log(arr); //[1,'b','b',4,3,3,4,5,1]
    console.log(arr.unique1()); //[1, "b", 4, 3, 5]
    //这种方法的主要思路就是,新建一个数组,然后在原数组中,从第一个开始,看看新数组里面有没有这个元素,如果有,就忽略,然后进行下一个,如果没有,则把这个元素存到新数组里面,
    //也就是说,每一次比较,都会遍历新数组,直到找到相同元素为止,比较耗性能

If you are not used to this writing method, you can change it to the following writing method, the effect is the same:

var arr = [1,'b','b',4,3,3,4,5,1];

function unique1(arr){
      var arr1 = [];
      for(var i=0;i<arr.length;i++){
        if(arr1.indexOf(arr[i]) == -1){//判断目标数组中在原数组里是否存在
          arr1.push(arr[i]); 
        } 
      } 
      return arr1;
    }
    console.log(arr); //[1,'b','b',4,3,3,4,5,1]
    console.log(unique1(arr)); //[1, "b", 4, 3, 5]

I will not rewrite the following method. You can rewrite it according to the above format. I will not output the result because the result is the same. The comments are written in the code. Take your time to experience it

The second type is achieved through hash tables (this concept is a bit big, the specific principles will not be elaborated here. I will write it separately when I have time. This is a good thing)

var arr = [1,'b','b',4,3,3,4,5,1];

Array.prototype.unique2 = function(){
      var hash = {}; //定义一个hash表
      var arr1 = []; //定义一个新数组
      for(var i=0;i<this.length;i++){
        /*
          这里比较难理解,我们一步一步来看:
          hash是一个对象,则存在键值对(key:value),只不过现在是为空的,所以hash[key] = value;
          第一步:i=0;this[i]=this[0]=1; hash[this[0]] = hash[1] , 因为hash初始为空,没有找到key=1的值,所以然后undefined,
          执行下一步:hash[1] = true(此时hash对象就有了第一组键值对),将原数组的第一个数添加到新数组中,重复第一步
          因为不重复的判断hash的值都是undefined,而重复的都为true了,所以不重复都被添加到新数组中
          因为hash表存的值是存的地址,放在堆内存中,所以有多少个不重复的元素,就要分多少个内存来存放,所以这种方法比较占内存,但是相比之下,这种的运算运动是最快的,
          这也就是用空间来换取时间了,数据量比较小,推荐用此方法
        */
        if(! hash[this[i]]){
          hash[this[i]] = true;
          arr1.push(this[i]);
        }
      }
      return arr1;  
    }
    console.log(arr);
    console.log(arr.unique2());


The third method is achieved by traversing whether its own position is consistent

var arr = [1,'b','b',4,3,3,4,5,1];

Array.prototype.unique3 = function(){
      var arr1 = []; //定义一个新数组
      for(var i=0;i<this.length;i++){
        if(this.indexOf(this[i])==i){
        //这里也是indexOf遍历,看从第一个元素在原数组中的位置,如果第一次出现的位置和下标相等,说明当前元素的不重复的,如果不等,说明该元素前面已经出现过
          arr1.push(this[i]);
        }
      }
      return arr1;  
    }
    console.log(arr);
    console.log(arr.unique3());


The fourth method, which is a bit interesting, can only be used in special occasions. It is to sort the array first, then compare 22, and output a new sorted array

Array.prototype.unique4 = function(){
      /*
        这里是思路是,先排序(默认从小到大),然后将原数组的第一个给新数组,
        因为是经过排序的,所以重复的只会存在在相邻位置
        这里就相当于是做22比较,如果相等,则进行下一组,如果不相等,则把这个数存到新数组中,用这个数再进行比较
      */
      this.sort();
      var arr1 = [this[0]];
      for(var i=1;i<this.length;i++){
        if(this[i] !== arr1[arr1.length-1]){
          arr1.push(this[i]);
        } 
      }
      return arr1;  
    }
    console.log(arr);
    console.log(arr.unique4());

Wow, let’s call it a day!

The requirements also say that it can be implemented using a double-layer nested loop. The other way around is to use a 2-layer for loop and compare each one with the original array

Array.prototype.unique5 = function(){
      //双层循环,一一比较
      for(var i=0;i<this.length;i++){ //从0开始
        for(j= i+1;j<this.length;j++){ //从1开始,逐个比较
          if(this[i] === this[j]){ //如果恒定
            this.splice(j,1);  //就将这个元素删掉
          } 
        } 
      }
      return this;  
    }
    console.log(arr);
    console.log(arr.unique5());

This writing method requires too many loops and is not recommended. Some people will say, don’t the first and third methods also need to be traversed every time? Is it similar to the fifth method? Yes, you can understand it this way, which means you understand it, but it is not a special understanding. If we say it is almost the same, it is too different. indexOf() means that when the first matching element is found, it will be

Stop traversing, and Type 5 will traverse the entire array regardless of whether it can be found. If the amount of data is large, which one do you think has better performance?

A special note: When comparing two values ​​that are congruent or unequal, be sure to use constant (===) and non-constant (!==), because this will involve elements In terms of type, for example, 1 and '1' are not identical!

The above is really dry information, there is no moisture at all, you can only rely on everyone to understand it!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool