JavaScript several arrays remove duplicate values method recommend _javascript tips

Source: Internet
Author: User

An array to repeat is a common requirement, and we temporarily consider the same type of array to repeat. The main reason is to clear the thinking and consider the performance. The following methods, the basic online have, here is simply summed up.

Ideas:

1. Iterate over the array, compare, compare to the same delete the following
2. Iterate over the array, compare, compare to the same, skip the previous repeats, and add the new array
3. Take an array element into the new array, iterate through the remaining array elements, and compare the elements of the new array, if different, put the new array.
4. Traverse the array, take an element, as the object's property, to determine whether the property exists

1. Delete the following duplicates:

function Ov1 (arr) {
  //var a1= (New Date). GetTime ()) for
  (Var i=0;i<arr.length;i++) for
    (Var j=i+1;j< arr.length;j++)
      if (Arr[i]===arr[j]) {arr.splice (j,1); j--;}      
  Console.info (New Date). GetTime ()-a1) return        
  arr.sort (function (a,b) {return a-b});

2. This is the normal method, better understanding, if the same, jump out of the loop

function Ov2 (a) {
  //var a1= (New Date). GetTime ())
  var b = [], n = a.length, I, J;
  for (i = 0; i < n; i++) {for
    (j = i + 1; j < N; j +)
      if (a[i] = = A[j]) {j=false;break;}
    if (j) B.push (A[i]);
    }
  Console.info (New Date). GetTime ()-a1) return  
  b.sort (function (a,b) {return a-b});
 

3. It took me a long time to understand where the J Loop went on, but the I value has changed. is equal to a new I loop:

function Ov3 (a) {
  //var a1= (New Date). GetTime ())
  var b = [], n = a.length, I, J;
  for (i = 0; i < n; i++) {for
    (j = i + 1; j < N; j +)
    if (a[i] = = A[j]) j=++i
  B.push (a[i));
  Console.info (New Date). GetTime ()-a1) return  
  b.sort (function (a,b) {return a-b});
   

4. Ensure that the new array is unique

function ov4 (AR) {
//var a1= (New Date). GetTime ())
  var m=[],f;
  for (Var i=0;i<ar.length;i++) {
  f=true; 
  for (Var j=0;j<m.length;j++)
  if (Ar[i]===m[j]) {f=false;break;};
  if (f) m.push (Ar[i])}
//console.info ((New Date). GetTime ()-a1) return  
  m.sort (function (a,b) {return a-b});

5. Use Object properties

	function Ov5 (AR) {
	//  var a1= (new Date). GetTime ()
			var m,n=[],o= {};
			for (var i=0; (m= ar[i])!==undefined;i++)
			if (!o[m)) {N.push (m); o[m]=true;}  Console.info (New Date). GetTime ()-a1) return  
		n.sort (function (a,b) {return a-b});
		

Above this javascript several arrays to eliminate the duplicate value the recommendation is the small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.