JavaScript array to redo the _javascript tips

Source: Internet
Author: User
Tags numeric javascript array

Objective

Recently in order to change the job, prepare for the interview, began to review the knowledge of JavaScript, yesterday afternoon thought of the array to weight related methods, simply sorted out a few JavaScript algorithm articles, for later use, this series of articles indefinite number, indefinite time, think of where to write, do not guarantee correctness, Do not guarantee high efficiency, just to talk about personal understanding, if there are mistakes, please treatise.

About going heavy

Array weight is a more common algorithm point, the way to achieve the weight is no more than through uniqueness and uniqueness. Simply to pick out the unique or delete the not unique. All of the following algorithms are named after me, please ignore them.

Loop match Go Heavy

As the name suggests, is the array of each element and the storage elements of the array contrast, encounter the elements of the new array, until the end of the loop, because the match also has a cycle, can also be called double loop matching to go heavy, this is the simplest way we can think of.

Implementation code:

var arr=[1,3,4,56,3,7,9,7];
var result=[];
Matching
function IsMatch (array,n) {for
  (var i=0;i<array.length;i++) {
    if (array[i]==n) {return
      true;
    }
  }
  return false;
Validate all element
function Unqiue (array) {for
  (var i=0;i<array.length;i++) {
    if (!ismatch (result,array[i)) ) {
      Result.push (array[i]);
    }
  return result;
};

Console.log (Unqiue (arr));

Note: The above method has a bug that does not distinguish between numeric and numeric strings when there is a string of numbers and numbers. Because in the matching function IsMatch () uses the double equal "= =", does not validate the element type, actually should use the congruent "= =".
The modified code is as follows:

var arr=[1,3,4,56,3, ' 1 ', 7,9,7];
var result=[];
Matching
function IsMatch (array,n) {for
  (var i=0;i<array.length;i++) {
    if (array[i]===n) {return
      true ;
    }
  }
  return false;
Validate all element
function Unqiue (array) {for
  (var i=0;i<array.length;i++) {
    if (!ismatch (result,array[i)) ) {
      Result.push (array[i]);
    }
  return result;
};

Console.log (Unqiue (arr));

Algorithm Advantages and Disadvantages:

Advantages:

Simple to achieve, intuitive thinking

Disadvantages:

Low efficiency

JSON go heavy/object go heavy/dictionary go heavy

JSON goes heavy by simply using the uniqueness of the object key to convert the elements of the array to JSON or the key value of the object. The value of JSON stores the index of the array, and then the JSON object is in a for loop and stored in the new array.

Array, JSON, {} are all object, so any one can implement this algorithm.

Implementation code:

Array mode:

var arr=[1,3,4,56,3, ' 1 ', 7,9,7];
function Unqiue (array) {
  var cache=[];
  var result=[];
   Converts an array element to the object's key for
  (Var i=0;i<array.length;i++) {
    cache[array[i]]=i;
  };
  
  Store key (actual array element) for
  (key in cache) {
    result.push (key);
  };
  
  return result;
}
  
Console.log (Unqiue (arr));

Json method:

var arr=[1,3,4,56,3, ' 1 ', 7,9,7];
function Unqiue (array) {
  var cache={};
  var result=[];
   Converts an array element to the object's key for
  (Var i=0;i<array.length;i++) {
    cache[array[i]]=i;
  };
  
  Store key (actual array element) for
  (key in cache) {
    result.push (key);
  };
  
  return result;
}
  
Console.log (Unqiue (arr));

Object method:

var arr=[1,3,4,56,3, ' 1 ', 7,9,7];
function Unqiue (array) {
  var cache=new Object ();
  var result=[];
   Converts an array element to the object's key for
  (Var i=0;i<array.length;i++) {
    cache[array[i]]=i;
  };
  
  Store key (actual array element) for
  (key in cache) {
    result.push (key);
  };
  
  return result;
}
  
Console.log (Unqiue (arr));

Algorithm Advantages and Disadvantages:

Advantages:

Simple

Efficiency is very high

Disadvantages:

1. Changed the type of the array element ()
2. Bug (no more than numeric and numeric type strings)

The queue is handed back heavy

I thought about it a long while ago. In the way of queues, the array is sorted into ascending or descending queues, so that the same elements are in one area, then the team tails forward, and if the match succeeds, delete the tail and then match the previous element to its front. After the entire match is complete, the remaining elements are the queues that are to be reset.

var arr=[6, 4, 6, 9, ' 6 ', ', ', 9,, ' One ', 1, 8, ' 7 ',, 5,, 3, 7];

function Unqiue (array) {
  //sorted array, forming queue
  array.sort (function (m,n) {return m-n;});
  After sorting, the team tails forward in contrast, if the same, delete the tail of the team, and then analogy
  function Loop (Index) {
    if (index>=1) {
      if (array[index]===array[ Index-1]) {
        arr.splice (index,1);
      }
      Loop (Index-1);
    }
    
  }
  Loop (array.length-1);
  return array;
}

Console.log (Unqiue (arr));

Algorithm Advantages and Disadvantages:

Advantages:

High efficiency

Disadvantages:

Efficiency is not the highest

IndexOf Way to go heavy

To determine if the browser supports INDEXOF, indexOf for ecmaScript5 new method IE8 the following (including IE8, IE8 only support part ECMA5) is not supported

if (! ARRAY.PROTOTYPE.INDEXOF) { 
//new IndexOf method 
Array.prototype.indexOf = function (item) { 
var result =-1, A_ item = NULL; 
if (This.length = = 0) {return result 
; 
} 
for (var i = 0, len = this.length i < len; i++) { 
a_item = this[i]; 
if (A_item = = Item) {Result 
= i; 
break; 
} 
} 
return result; 
} 
 

Related Article

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.