Common algorithms implemented by JS and common algorithms implemented by JS

Source: Internet
Author: User

Common algorithms implemented by JS and common algorithms implemented by JS

(1) array deduplication

Principle: Define an object obj, and use the array element as the property name of obj to determine whether to duplicate the attribute name.

Var unique = function (arr) {let obj = {}; let newArr = []; arr. forEach (function (x) {if (! Obj [x]) {// if the object does not have the property obj [x] = true; newArr. push (x) ;}}; return newArr;} corresponding to this element ;}

(2) Sort arrays using a quick Sorting Algorithm

There are two types of effects. One is to use the feature of fast sorting to achieve fast sorting with no duplicates, and the other is fast sorting without duplicates.

Principle: Obtain the target array, select the most flag of an element, traverse the remaining elements, put them on the right side larger than the flag, and put them on the left.

Note: There are also elements that are equal to the flag bit. If you store equal elements, de-duplication is achieved. If you store the same elements, de-duplication is eliminated.

Var quickSort = function (arr) {if (arr. length <= 1) {return arr;} // defines a left array and a right array let leftArr = []; let rightArr = []; // select a reference value, let tag = arr [0];/** use the following method to determine and remove duplicate elements, this achieves simultaneous deduplication of fast sorting */for (let I = 0; I <arr. length; I ++) {if (arr [I] <tag) {// place the element smaller than the tag in the left array leftArr. push (arr [I]);} if (arr [I]> tag) {// place the element larger than the tag in the right array. push (arr [I]) ;}}/** use the following method to sort data by using a fast sort, without duplicates */for (let I = 1; I <arr. length; I ++) {if (arr [I] <tag) {// place the element smaller than the tag in the left array leftArr. push (arr [I]);} else {// place the element larger than the tag in the right array. push (arr [I]) ;}// call return [] recursively. concat (quickSort (leftArr), [tag], quickSort (rightArr ));}

(3) count the most frequently occurring characters in a string

Principle: This is similar to array deduplication. It also uses an object obj to take the array element as the attribute name of the object. If this attribute name does not exist, the value is assigned to 1. If so, the value is added to 1.

Var maxShowTimes = function (str) {// create an object for Weight Determination let obj ={}; // judge whether the string is null or only one element if (str. length <= 1) {return str. length = 0? 'String cannot be blank ': str;} // obtain each character for (let I = 0; I <= str. length; I ++) {if (! Obj [str. charAt (I)]) {// If obj [str. charAt (I)] = 1;} else {// If obj [str. charAt (I)] + = 1 ;}// In the obj object, find the attribute that has the largest value, let maxChar = ''; let maxTimes = 0; for (var k in obj) {if (obj [k]> maxTimes) {maxChar = k; maxTimes = obj [k] ;}} return maxChar ;}

(4) Exchange Values of two variables without using the third variable

Principle: it is to replace a variable. The idea is clever and can only be used for the exchange of numbers.

Var swap = function (a, B) {if (a = B) {return [a, B];} B = B-; // here, the values of B and a in B-a are the initial values a = a + B; // a = a + B-; the value of B is assigned to a B = a-B; // B = a-(B-a) = 2a-B is equivalent to 2b = 2a; the value of a is assigned to B return [a, B];}

(5) calculate the maximum difference value of an array

Principle: traverse the array once, find the maximum and minimum values, and return the difference value

Var getMaxProfit = function (arr) {// defines two variables, respectively storing the maximum and minimum values let maxNum = arr [0]; let minNum = arr [0]; for (let I = 0; I <arr. length; I ++) {if (arr [I]> maxNum) {maxNum = arr [I];} if (arr [I] <minNum) {minNum = arr [I] ;}return maxNum-minNum ;}

(6) Obtain random strings of any length

Principle: You can manually specify the Character Library and random character length n, and use Math. floor () and Math. random () to obtain random characters.

Var getRandomString = function (n) {// define the random string Character Library let str = 'qwertyuiopasdfghjklzxcvbnm1234567890 '; // define a temporary variable tmp to store the generated random string let tmp = ''; // obtain the str length let len = str. length; // generate a random string of n for (let I = 0; I <n; I ++) {tmp + = str. charAt (Math. floor (Math. random () * len);} return tmp ;}

The above is a few common JS algorithms introduced by the editor. I hope it will help you. If you have any questions, please leave a message. The editor will reply to you in time, thank you very much for your support for the help House website!

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.