Home >Web Front-end >JS Tutorial >Algorithm for deduplicating data_Basic knowledge

Algorithm for deduplicating data_Basic knowledge

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 19:23:571083browse

I encountered such a problem at work: given a sorted array (ascending order), delete duplicate data in the array, but only one array can be used, and the size of this array can change.
Example: Array: [1 ,1,2,2,3,3,4,5,7,10]
Output::[1,2,3,4,5,7,10]
Implementation (1):
var arr =new Array(1,1,2,2,3,3,4,5,7,10);
var len = arr.length;
for(var i=len-1; i>=1;i--)
{
if(arr[i-1] == arr[i])
{
arr.splice(i,1);

}
alert(arr);

Implementation (2):
var arr =new Array(1,1,2,2,3,3,4,5,7 ,10);var len = arr.length;var k=0;for(var i=1;i

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