Array Common Tools

Source: Internet
Author: User
Tags repetition

//Pass an array into the function and return a new array that holds the number of repetitions of each element in the first array.
function fa (arr) {
var num;
var arra = [];
For (var i = 0; i < arr.length; i++) {
num = 0;
For (var j = 0; J < Arr.length; J + +) {
if (arr [j] = = arr [i]) {
num++
                                           };
                             };
Arra.push (num);
                   };
Console.log (ARRA);
           };
var a = [1, 2, 3, 3, 2, 1, 3,-2,-2, 1];
FA (a); [3, 2, 3, 3, 2, 3, 3, 2, 2, 3]



//Pass an array into the function and return a new array that holds the elements in the first array for repetition.
function fb (arr) {
var num;
var arra = [];
For (var i = 0; i < arr.length; i++) {
num = 0;
For (var j = 0; J < Arr.length; J + +) {
if (arr [j] = = arr [i]) {
num++
                                       };
                           };
if (num > 1) {
Arra.push ("repeat")
} else {
Arra.push ("no Repetition")
                             };
                   };
Console.log (ARRA);
          };
var b = [1, 2, 3, 4, 5, 3, 1];
FB (b);//["Repeat", "no Repetition", "repeat", "no Repetition", "no Repetition", "repeat", "repeat"]


//Determine if there is an element in an array, and if so, it appears several times.
function FC (n, arr) {
var num = 0;
For (var i = 0; i < arr.length; i++) {
if (arr [i] = = N) {
num++
                                };
                    };
if (num > 0) {
Console.log ("There is this element, it appears" + num + "Times")
} else {
Console.log ("Without this element")
                     };
           };
var c = [0, 1, 2, 3, 4, 5, 2, 5, 1, 2, 3];
FC (2, C);//With this element, it appears 3 times


//To find the maximum value in an array.
function fd (arr) {
var num = arr [0];
For (var i = 1; i < arr.length; i++) {
if (num < arr [i]) {
num = num ^ arr [i];
arr [i] = num ^ arr [i];
num = num ^ arr [i];
                               };
                      };
console.log (num);
           };
var d=[0,1,33,32,16,12,8,4];
FD (d);//33


//An array is arranged in order from small to large.
function Fe (arr) {
For (var i = 0; i < arr.length-1; i++) {
For (var j = i + 1; j < Arr.length; J + +) {
if (arr [i] > arr [j]) {
arr [i] = arr [i] ^ arr [j];
arr [j] = arr [i] ^ arr [j];
arr [i] = arr [i] ^ arr [j];
                                          };
                                  };
                     };
Console.log (arr);
           };
var e = [1, +, +, -10, 0];
Fe (e);//[-10,0,1,12,15,17,23,26,28]

Array Common Tools

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.