JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

1,226,326 Responses

  1. Покер-рум Pokerdom начал свою работу в 2014 году, основная направленность – страны бывшего СНГ.

  2. Rickysem表示:

    pharmacy website india buy medicines from India reputable indian pharmacies

  3. Marcelabelp表示:

    https://mexicoph24.life/# reputable mexican pharmacies online

  4. Replace表示:

    You’ll Never Guess This Renault Kadjar Replacement Key’s
    Tricks Replace

  5. With havin so much written content do you ever run into any
    issues of plagorism or copyright infringement? My blog has a lot of unique content I’ve either
    authored myself or outsourced but it appears a lot of it is popping it
    up all over the web without my permission. Do you know any techniques
    to help protect against content from being
    ripped off? I’d genuinely appreciate it.

  6. Rickysem表示:

    online shopping pharmacy india indian pharmacy indianpharmacy com

  7. Porsche Key Shell Tools To Make Your Daily Lifethe One Porsche Key Shell
    Trick That Every Person Must Be Able To Porsche Key Shell

  8. Stevenrip表示:

    canadian pharmacy: canadian pharmacies – legal canadian pharmacy online

  9. Five Killer Quora Answers On Secondary Double Glazing Near
    Me Double glazing near Me

  10. JustinCit表示:

    india pharmacy mail order http://indiaph24.store/# buy prescription drugs from india
    india online pharmacy

  11. Marcelabelp表示:

    http://indiaph24.store/# indian pharmacy online

  12. You’ll Never Guess This Double Glazing Repair Leeds’s Secrets Double glazed Window repairs
    yogicentral.science

  13. Aw, this was a very good post. Taking the time and actual effort to generate a really good article… but what can I say… I procrastinate a whole lot
    and don’t manage to get nearly anything done.

  14. The 10 Most Scariest Things About Leeds Double Glazing Leeds Double Glazing

  15. Guide To Auto Key Locksmith: The Intermediate Guide
    To Auto Key Locksmith Auto Key locksmith

  16. Marcelabelp表示:

    http://mexicoph24.life/# buying from online mexican pharmacy

  17. Devfolio.Co表示:

    Good day very nice web site!! Man .. Excellent ..
    Amazing .. I will bookmark your blog and take the feeds also?
    I am glad to search out numerous helpful info here within the post, we need develop more
    techniques on this regard, thank you for sharing.
    . . . . .

  18. Buzzwords De-Buzzed: 10 Different Ways Of Saying Buy Mobility Scooter Near Me best foldable mobility scooter uk

  19. Why Double Glazing Companies Near Me Is Relevant 2023 Replacing Double Glazed Windows

  20. Marcelabelp表示:

    https://indiaph24.store/# world pharmacy india

  21. Why CS GO Weapon Case Will Be Your Next Big Obsession Esports 2013 winter case

  22. At this moment I am ready to do my breakfast, when having
    my breakfast coming yet again to read additional news.

  23. Marcelabelp表示:

    http://indiaph24.store/# pharmacy website india

  24. JustinCit表示:

    world pharmacy india https://indiaph24.store/# buy medicines online in india
    top online pharmacy india

  25. Testsieger表示:

    Toyota Highlander 4-cylinder and Hybrid: Test Hybrid averaged 26.3 mpg
    in a mixture of city and highway driving. Those texting while driving are six instances
    more more likely to be in an accident than these driving drunk.
    Cell phones are fantastic devices that keep us in touch with
    our mates and cherished ones throughout the day, but there
    are times when the phone must be put away. Though there is a class
    of automotive drivers who swear by them, the handbook transmission has seen better days.
    It’s not one-of-a-sort, although, since there is also a spare.
    Not all of these innovations were optimistic, although, with a number of situations of
    death and tragedy. It is a prison warden, though, who speaks the famous line.
    Feasibility The line Bisection Test takes solely 5 minutes
    to finish and is easy to score. U.S. News 360
    Evaluations takes an unbiased method to our suggestions. It’s the first import to sell 1 million items in the U.S.
    The Chevy Impala set the bar for automobile gross sales in 1965 when 1,074,925 items of
    the Impala and Impala SS were bought.

  26. Starter Kit Avon Explained In Fewer Than 140 Characters avon starter Kit

  27. Rickysem表示:

    online canadian pharmacy reviews Large Selection of Medications from Canada canadian pharmacy in canada

  28. 5 Killer Quora Answers On Double Glazed Window Repairs Near Me Double glazed window repairs near me

  29. Pedroereva表示:

    mexican drugstore online: Online Pharmacies in Mexico – medication from mexico pharmacy

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。