Deep understanding of JS for fast sorting and de-duplication, deep understanding of js for sorting

Source: Internet
Author: User

Deep understanding of JS for fast sorting and de-duplication, deep understanding of js for sorting

JS's quick sorting and JS's focus on the interview are quite a few questions. below is my understanding of quick sorting, and the Code for quick sorting and deduplication.

1. What is quick sorting?

Step 1: Fast sorting is to remove the median value. If the median value is smaller than the median value, set it to arrLeft on the left. If the median value is greater than the median value, set it to arrRight on the right.

Step 2: Perform the first step on arrLeft and perform the first step on arrRight. (This is obviously a recursion. It ends when the array length is smaller than 2)

Step 3: Merge arrLeft, median, and arrRight

quickSort = function(arr){if(arr.length < ){return arr;}var tmp = arr.splice(Math.floor(arr.length/), )[],arrLeft = [],arrRight = [];for(var i = ; i < arr.length; i++){if(arr[i] >= tmp){arrRight.push(arr[i]);}else{arrLeft.push(arr[i]);}}return arguments.callee(arrLeft).concat(tmp,arguments.callee(arrRight));}

2. How does JS remove duplicates?

The first thing we think of is to repeat an array. Every time we get a value, we will compare it with the following one. If there is not the same one, we will put it into a new array, so we will remove the duplicates.

First

var unique = function(arr){var newArr = [];while(arr.length){var value = arr.shift();for(var i = , len = arr.length; i<len; i++){if(value == arr[i]){break;}}if(i == len){newArr.push(value);}}return newArr;}

However, this process repeats twice, and the efficiency is not good. The following provides better results. Declare an object. The value of the array is used as the attribute of the object and assigned as one. Then, you can determine whether this object attribute exists.

Second

var unique = function(arr){var arr = [];var obj = {};for(var i = , len = a.length; i < len; i++){if(!obj[a[i]]){obj[a[i]] = ;arr.push(a[i]);}}console.log(obj)return arr;};

Another method is sorting and removing duplicates, which makes it easier to determine whether the previous value is equal to the next value.

Third

var unique = function(arr){var newArr = [];for(var i = , len = arr.length; i <len; i++){if(arr[i] !== arr[i+]){newArr.push(arr[i]);}if(i == len){newArr.push(arr[i]);}}return newArr;}

The above section describes how to quickly sort and deduplicate JavaScript files. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.