7, array de-weight and sorting

Source: Internet
Author: User

First, the array to weight

    /** * Array de-weight * 1, double cycle method-determine if there is an equal item * Note: The second loop starts with i+1 **/Let circlerepeat=function (ary) { for(Let i=0; i<ary.length;i++) {Let cur=Ary[i];  for(Let j=i+1; j<ary.length;j++){                if(cur = = =Ary[j]) {Ary.splice (J,1); J--; }            }        }        returnary};//Console.log (Circlerepeat ([1,2,3,4,1,1,2,2,3,4,4]));    /** * Array de-weight * 2, new array method-new array No current item-put new array * Note: indexOf usage **/Let newarrrepeat=function (ary) {Let Ary2= [];  for(Let i=0; i<ary.length;i++) {Let cur=Ary[i]; if(Ary2.indexof (cur) = = =-1) {ary2.push (cur); }        }        returnAry2};//Console.log (Newarrrepeat ([1,2,3,4,1,1,2,2,3,4,4]));    /** * Array de-weight * 3, sort order-determine if two adjacent items are equal after sorting * Note: Sort usage and prevent array collapse **/Let sortrepeat=function (ary) {Ary.sort (A, b) {returnA-C;        });  for(Let i=0; i<ary.length;i++){            if(Ary[i] = = = ary[i+1]) {Ary.splice (I,1); I--;//Prevent array collapse            }        }        returnary; };//Console.log (Sortrepeat ([1,2,3,4,1,1,2,2,3,4,4]));    /** * Array deduplication * 4, object Method-to see if there are duplicates in the object, if any, delete the array the item * Note: The object's property name and property value **/Let objrepeat=function (ary) {Let obj={};  for(Let i=0; i<ary.length; i++) {Let cur=Ary[i]; if(obj[cur]==cur) {Ary.splice (i,1); I--; } Obj[cur]=cur; }        returnary; };//Console.log (Objrepeat ([1,2,3,4,1,1,2,2,3,4,4]));    /** * Array de-weight * 5, the object does not duplicate the name of the property-the use of the object does not duplicate the attribute can not only go to the weight-property names; and the property value also records the number of occurrences of the current element * Note: object's property name and property value **/Let objnamerepeat=function (ary) {Let obj={},ary2=[];  for(Let i=0; i<ary.length; i++) {Let cur=Ary[i]; /*if (Obj[cur]) {obj[cur]++;             Continue } obj[cur]=1; Adds the contents of the array to the object and assigns a value of 1;*/            if(Obj[cur]) {Obj[cur]++; }Else{Obj[cur]=1; }        }        returnobj; };//Console.log (Objnamerepeat ([1,2,3,4,1,1,2,2,3,4,4]));

Second, array sorting

    /** * Quick Sort * 1, find middle position and corresponding value * 2, each item in the array is compared with the median value left[] right[] * 3, recursive + array concatenation **/Let quickSort=function (ary) {if(ary.length<=1){            returnary} let num= Math.floor (ary.length/2); Let NumValue= Ary.splice (num,1)[0]; Let left= [],right = [];  for(Let i=0; i<ary.length;i++) {Let cur=Ary[i]; if(cur<NumValue) {Left.push (cur)}Else{right.push (cur)}}returnQuickSort (left). Concat ([Numvalue],quicksort (right))};//Console.log (QuickSort ([2,43,7,89,0,6,4,2,5,6]))    /** * Insert sort * 1, take the corresponding value of the first array to compare.     * 2, the loop original array and the left array * 3, the original array the current item is less than each item in leave, put it to the front.     * 4. The current item of the original array is greater than one of the items in left and is placed after this item. * 5, return to left **/Let insertsort=function (ary) { let left= Ary.splice (0,1);  for(Let i=0; i<ary.length;i++) {Let cur=Ary[i];  for(Let j=left.length-1; j>=0;){                if(cur<Left[j]) {J--; if(j = =-1) {left.unshift (cur); }                }Else{Left.splice (J+1,0, cur);  Break; }            }        }        returnLeft };//Console.log (Insertsort ([2,43,7,89,0,6,4,2,5,6]))    /** * Bubble sort * 1, the first loop--the length of the array = Compare the number of wheels Ary.length * 2, the second loop--the number of times per round comparison ary.length-1-i (last item not compared) * 3, each The second comparison puts the larger value back-and each wheel compares the maximum value to the last * 4, returning the array **/Let bubblesort=function (ary) { for(Let i=0; i<ary.length;i++){             for(Let j=0; j<ary.length-1-i;j++){                if(ary[j]>ary[j+1]) {Let TMP= ary[j+1]; Ary[j+1] =Ary[j]; ARY[J]=tmp; }            }        }        returnary};//Console.log (Bubblesort ([2,43,7,89,0,6,4,2,5,6]));

7, array de-weight and sorting

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.