JavaScript Learning record--array de-weight

Source: Internet
Author: User

 vararr = [1, 2, 3, 5, 5, ' 45 ', ' 45 ', 4, 1, ' 1 ', ' 2 ']     for(vari = 0; I < 10000; i++) {Arr.push (i)//This adds 10000 entries to arr, allowing you to view the performance of each method later    }    /** Method One: Borrowing an array of results, the elements in the original array are compared to the result array, and if there are no elements congruent with the original array, the original array elements are stored in the result array. * @param arr Incoming array * @returns {array} returns the array after the redo*/    functionUsearr (arr) {//Encapsulation Functions        varStart = +NewDate ()//get the initial time        varResultarr = []//Create a new Resultarr to hold non-repeating elementsResultarr.push (Arr[0])//in Resultarr, you can add an element from arr, guaranteeing that the back can be entered into the inner for loop         for(vari = 1; i < arr.length; i++) {//Traverse arr, starting with the element indexed as 1            varFlag =true//This is used to declare a variable using the assumption method, assuming that item I in ARR is not equal to each item in Resultarr.              for(varj = 0; J < Resultarr.length; J + +) {//Traverse Resultarr                if(Arr[i] = = = Resultarr[j]) {//Compare section I of ARR with each item in ResultarrFlag =false//If there is an element in Resultarr that is congruent with the arr. I, the assumption is not true, so that flag is false,                     Break//now that you have duplicate entries, the other items don't have to be compared, just end the inner for Loop .                }            }            if(flag) {//if it is assumed that there are no elements in Resultarr that are congruent with arr I, then arr is added to ResultarrResultarr.push (Arr[i])}} varEnd = +NewDate ()//Get end TimeConsole.log (End-start)//output time difference is approximately 260ms        returnResultarr//returns an array of results} console.log (Usearr (arr))/** Method Two: The element in ARR is compared to the following element, and if there are elements that are congruent with it, delete the following element * @param arr * @returns {Array}*/    functionUSEARR1 (arr) {//Encapsulation Functions        varStart = +NewDate ()//get the initial time         for(vari = 0; i < arr.length; i++) {//Traverse arr             for(varj = i + 1; J < Arr.length; J + +) {//Traverse All items following the item I                if(Arr[i] = = = Arr[j]) {//Compare all items after item I withArr.splice (j, 1)//If the existence of subparagraph j is congruent with subparagraph I, delete subparagraph Jj--//because deleting a j,j+1 item will change to a J, in order to make the later comparison smooth, it is necessary to j--                }            }        }        varEnd = +NewDate ()//Get end TimeConsole.log (End-start)//output time difference is approximately 193ms        returnArr//returns the original array after the redo} console.log (USEARR1 (arr))/** method Three: Use IndexOf to find out if there are elements equal to arr in Resultarr, and if not, place the elements of arr into Resultarr * @param arr Incoming array * @returns {array} The returned array of the de-heavy*/    functionUSEARR2 (arr) {//Encapsulation Functions        varStart = +NewDate ()//get the initial time        varResultarr = []//Create a new Resultarr that holds non-repeating elements         for(vari = 0; i < arr.length; i++) {//Traverse arr            if(Resultarr.indexof (arr[i]) = =-1) {//look for an element in Resultarr that is equal to the item I of ARR, with a result of-1 indicating that it does not exist (IndexOf is looking for congruent elements)Resultarr.push (Arr[i])//If there is no element equal to ARR item I in the result array, the item I of ARR is stored in Resultarr            }        }        varEnd = +NewDate ()//Get end TimeConsole.log (End-start)//output time difference is approximately 244ms        returnResultarr//returns an array of results} console.log (USEARR2 (arr))/** method Four: The elements in the array are compared to their own elements, skipping the lvalue with duplicates, each time taking no more than the right value of the duplicates, * @param arr incoming array * @returns {array} returns the array after the deduplication*/    functionUSEARR3 (arr) {//Encapsulation Functions        varStart = +NewDate ()//get the initial time        varResultarr = []//Create a new Resultarr, save the element after the heavy         for(vari = 0; i < arr.length; i++) {//Traverse arr             for(varj = i + 1; J < Arr.length; J + +) {//Remove the comparison following item I                if(Arr[i] = = =Arr[j]) {J= ++i//If there is an element that is congruent with item I after subparagraph I, skip item I, starting with i+1, and comparing again (although the inner loop continues                           //but the outer layer is equivalent to the next I loop) until the inner for loop is gone and there is still no congruent element.}} resultarr.push (Arr[i])//if the element after item I is congruent with it, then add the item I of ARR to the Resultarr        }        varEnd = +NewDate ()//Get end TimeConsole.log (End-start)//output time difference is approximately 214ms        returnResultarr//returns the array after the redo} console.log (USEARR3 (arr))/** method Five: Use an object to find out if there is an attribute in the object as an array element, and if not, add the element to the object's properties, * because the property name inside the brackets is looked up as a string, so the value 1 and the string ' 1 ' are duplicated , you can save the type value of the value element as the attribute value in the corresponding attribute * @param arr Incoming value * @returns {Array} returns the value after*/    functionUSEARR4 (arr) {//Encapsulation Functions        varStart = +NewDate ()//get the initial time        varobj = {}, Resultarr = [];//creates a new empty object, compares it, and saves the non-repeating element with an empty array         for(vari = 0; i < arr.length; i++) {//Traverse arr            varType =typeofArr[i]//declares a variable that holds the type of the value of the item I element in the array            if(!obj[arr[i]]) {//judgment: If the array element attribute in the object does not existObj[arr[i]] = Type//then save the array element as a property of the object and save the type of the array element value as a property valueResultarr.push (Arr[i])//add an array element that satisfies the condition to the Resultarr}Else if(Obj[arr[i]].indexof (type) = =-1) {//then judge: all ARR elements that do not meet the previous if condition enter into the else if, and find the attribute value as the object's property                //If there is no property value for the property that is equal (not congruent) with it, that is, the element that exists in the array that is equal but not complete, the element is added to the ResultarrResultarr.push (Arr[i])}} varEnd = +NewDate ()//Get end TimeConsole.log (End-start)//output time difference is approximately 3ms        returnResultarr//returns the array after the redo} console.log (USEARR4 (arr))//method Five performs best, but creates a new object that takes up space-memory, space-changing time

JavaScript Learning record--array de-weight

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.