What are the 5 methods to remove duplicates from arrays?
5 ways to remove duplicates from arrays:
Method 1:
Double for loop Deduplication
Principle: If two pairs are compared, delete the second one
For example: 1 1 1 3 2 1 2 4
Let the first 1 be arr [0] Compare with the following ones one by one. If the following value is equal to arr[0], delete the following value
The result after the first end is 1 3 2 2 4, delete all the following 1
Similarly, the second and third times will delete the same elements as yourself
function noRepeat1(arr){ // 第一层for用来控制循环的次数 for(var i=0; i<arr.length; i++){ //第二层for 用于控制与第一层比较的元素 for(var j=i+1; j<arr.length; j++){ //如果相等 if(arr[i] == arr[j]){ //删除后面的 即第 j个位置上的元素 删除个数 1 个 arr.splice(j,1); // j--很关键的一步 如果删除 程序就会出错 //j--的原因是 每次使用splice删除元素时 返回的是一个新的数组 // 这意味这数组下次遍历是 比较市跳过了一个元素 /* 例如: 第一次删除后 返回的是 1 1 3 2 1 2 4 * 但是第二次遍历是 j的值为2 arr[2] = 3 * 相当于跳过一个元素 因此要 j-- * */ j--; } } } return arr; }
Method 2:
Single-layer for loop
Principle Similar to method one
function norepeat(arr){ arr.sort(); //先排序让大概相同的在一个位置,这里为什么说是大概相同 是因为sort排序是把元素当字符串排序的 它和可能排成 1 1 10 11 2 20 3 ... 不是我们想要的从小到大 for(var i = 0; i < arr.length-1;i++){ //还是两两比较 一样删除后面的 if(arr[i]==arr[i+1]){ arr.splice(i,1); //i-- 和j--同理 i--; } } return arr; }
Method three:
Principle: Use an empty array to store the first appearing element
Use the indexOf attribute indexOf to return a specified The position where the character appears in the string. If not, -1 will be returned.
So we can make good use of this property. When -1 is returned, it will be stored in the array.
function noRepeat2(arr){ var newArr = []; for(var i = 0; i < arr.length; i++){ if(newArr.indexOf(arr[i]) == -1){ newArr.push(arr[i]); } } return newArr; }
Method 4:
Principle: Use the idea of the object. If there is no such attribute in the object, undefined will be returned.
Use this principle to put it into the array when the returned value is undefined. When assigning a value to this attribute
function norepeat3(arr) { var obj = {}; var newArr = []; for(var i = 0; i < arr.length; i++) { if(obj[arr[i]] == undefined) { newArr.push(arr[i]); obj[arr[i]] = 1; } } return newArr; }
Method 5:
Principle: If the loop comparison is equal, the value of the following elements will be 0, and finally deleted to 0 during output The premise is that there cannot be 0 in your data, but everything can be flexible. You can set any value to replace this 0. This method was what I thought of implementing at the time, so it was not well optimized.
var newArr = []; //控制外循环 for(var i=0; i<arr.length-1;i++){ //内存循环 只比较后面的 for(j=i+1;j<arr.length;j++){ //如果相等就让其值等于0 if(arr[i]==arr[j]){ arr[j]=0; } } //去除值为0的 if(arr[i]==0){ continue; }else{ //放入新的数组 newArr.push(arr[i]); } }
Recommended tutorial: "PHP Video Tutorial"
The above is the detailed content of What are the 5 methods of deduplicating arrays?. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools