Go to heavy and array sort

Source: Internet
Author: User
Tags array sort

Removes duplicates from the array.

The first method of using for loop

Defines the array var a = [2,3,3,4,5,3,2,6,7,3];

Define an empty array var b=[]

<script type= "Text/javascript" >
var a = [2,3,3,4,5,3,2,6,7,3];
var b = []; for (Var i=0;i<a.length;i++) {var s=0for (var j=0;j<b.length;j++) {if (A[i]==b[j]) {S=1}}if (s==0) {B.push (A[i])}} Alert (b) </script>

The second method of using functions

Defines the array var a = [2,3,3,4,5,3,2,6,7,3];

Define an empty array var b=[]

<script type= "Text/javascript" >var a = [2,3,3,4,5,3,2,6,7,3];var b = []; C (a); function C (a) {for (Var i=0;i<a.length;i++) {if (A.indexof (A[i]) ==i) {B.push (A[i])}}}document.write (b) </ Script>

Three-array sort bubble sort

Sort 7,8,9,3,2,1,4,6,4 from small to large

<script type= "Text/javascript" >       var attr=[7,8,9,3,2,1,5,6,4]var zj=0for (var i=0;i<attr.length-1;i++) { for (Var j=0;j<attr.length-1;j++) {if (attr[j]>attr[j+1]) {Zj=attr[j]attr[j]=attr[j+1]attr[j+1]=zj}}}alert ( attr) </script>

Go to heavy and array sort

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.