JavaScript fun: All sort de-duplication

Source: Internet
Author: User
Given a string, all its fully arranged results are displayed in an array, requiring no repeated results. For example, if a string is given and all its fully ordered results are displayed in an array, no repeated results are required.

For example:

I have a string named "aabb", which should have 4*3*2*1 = 24 results, but considering that there are no duplicates, 6 results are returned, as shown below:

['Aab', 'abab', 'abba', 'baab', 'baba ', 'bbaa']

Therefore, the key to the problem lies in two aspects:

1. How to perform full sorting

2. How to deduplicate results

For full permutation, recursive or non-recursive methods can be used.

Deduplication can be achieved using a hash.

// Recursively solve the full-permutation function permutations (string) {// hash var hash ={} for storing de-duplicated results; // traverse function // from: character array to be traversed // to: String Array of the record path var traverse = function (from, to) {// if the current depth does not reach the leaf if (. length <string. length) {for (var I = 0; I

 

  

The above are JavaScript interesting questions: the content is fully arranged and de-duplicated. For more information, please follow the PHP Chinese Network (www.php1.cn )!

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.