Home >Web Front-end >JS Tutorial >What is the method to remove duplicates from js array?

What is the method to remove duplicates from js array?

coldplay.xixi
coldplay.xixiOriginal
2021-03-15 16:34:1422138browse

js array deduplication method: 1. Array traversal method; 2. Array subscript judgment method; 3. Adjacent removal method after sorting; 4. Optimized array traversal method; 5. Array traversal method.

What is the method to remove duplicates from js array?

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

js array deduplication method:

The first one: traverse the array method

This method is the simplest and most intuitive , and the easiest to understand, the code is as follows:

What is the method to remove duplicates from js array?

This method is easy to understand, using the indexOf() method (indexOf() method if query Returns the index of the first result found in the array, if not found, returns -1). First create a new empty array to store the new deduplicated array, and then traverse the arr array. During the traversal process, determine whether there are elements in the traversed arr in the newArr array. If not, add them directly to newArr. , if it already exists (duplicate), then no operation is performed, and then it is traversed from beginning to end, which exactly achieves the purpose of deduplication.

Second type: Array subscript judgment method

This method is also easier to understand. The code is as follows:

What is the method to remove duplicates from js array?

This overlaps with the first method. Let’s not say anything redundant. Just look at if. During the process of traversing arr, if you find the current value in the arr array, the returned index is equal to i in the current loop. , then it proves that this value appears for the first time, so it is pushed into the new array. If a value that has appeared is later traversed, its index will not be returned. The indexof() method only returns the first one found. The index of the value, so duplicates will be passed, and values ​​that appear only once are stored in the new array, which also achieves the purpose of deduplication.

The third method: adjacent removal method after sorting

This method uses the sort() method, the code is as follows:

What is the method to remove duplicates from js array?

The idea of ​​this method is: first use the sort() method to sort arr, then after sorting, the same ones must be next to each other, just remove them, first initialize the new array An arr[0], because we need to use it to compare with the arr array, so i also starts from 1 in the for loop. We compare the value in arr traversed with the last bit of the new array. If they are equal, Then pass out, if they are not equal, push in, because the array has been reordered, and the duplicates are next to each other. This ensures that only the first of the duplicate values ​​will be pushed in, and the rest will be combined with the new array. If the elements pushed in are equal, they will be passed away, which also achieves the effect of deduplication.

Fourth: Optimized array traversal method

What is the method to remove duplicates from js array?

Idea: two layers of for loops, the outer layer controls the traversal to The element in the previous arr, the inner layer controls the elements after the element accessed by the first layer, starting from the 0th element, and comparing the 0th element with the element after it, if it is not equal to this element , it proves that there is no duplication, push it into a new array and store it. If there is an element equal to this element, pass it and enter the next loop directly. Starting from the first one, continue to compare with the elements behind it, proceed as above, and loop until the end: all non-repeating elements are pushed into the new array, while the previous elements that are repeated are passed away, leaving only If the last element is removed, it will no longer be repeated at this time, and a new array will be pushed, and all repeated elements will be filtered out, achieving the purpose of deduplication.

The fifth method: Array traversal method

What is the method to remove duplicates from js array?

Idea: It is also a two-layer for loop, and the outer for loop controls arr Array traversal, the inner for loop controls the traversal of the new array, starting from position 0, if the new array does not have the element traversed in the arr array, then the value of the state variable bl is still true, then naturally enter Push this value into the new array in if. If there is this element, it means repetition, then change the value of state variable bl to false, and jump out of the current loop. It will not enter the inside of if, but enter the next outer layer. Start the cycle. This cycle repeats, and finally the effect of deduplication is achieved.

Related free learning recommendations: js video tutorial

The above is the detailed content of What is the method to remove duplicates from js array?. For more information, please follow other related articles on the PHP Chinese website!

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