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,233,299 Responses

  1. The Advanced Guide To Adults Bunk Beds Deals bunk beds

  2. See What Replacement Bmw Key Fob Tricks The Celebs Are Using replacement bmw Key fob

  3. You’ll Never Guess This Coffee Machine Espresso’s Tricks coffee machine espresso

  4. JustinCit表示:

    buy prescription drugs from india https://indiaph24.store/# pharmacy website india
    Online medicine order

  5. Rickysem表示:

    pharmacy website india indian pharmacy fast delivery indianpharmacy com

  6. 自分のダッシュボード画面では投稿したイラストオナドールに対するリアクションが確認できるので、モチベーションにもつながりますよ。

  7. Corey表示:

    The Comprehensive Guide To Combo Washer washing machine sale (Corey)

  8. 30 Inspirational Quotes On Uk Online Shopping Sites Like Amazon 9.53Mm X 4.90M Label Tape

  9. One Key Trick Everybody Should Know The One Treadmills Trick Every Person Should Learn Treadmills at Home

  10. Rickysem表示:

    canadian pharmacy world Licensed Canadian Pharmacy ed drugs online from canada

  11. 5 Killer Quora Questions On Replacement Car Key lost car keys replacement cost

  12. Michaelcew表示:

    https://mexicoph24.life/# mexican mail order pharmacies

  13. 20 Fun Details About Adult Toys Men Adult Toys For Men

  14. Pedroereva表示:

    mexico pharmacy: mexico pharmacy – purple pharmacy mexico price list

  15. Many Of The Most Exciting Things Happening With Mesothelioma
    Lawyer Asbestos Litigation

  16. Marcelabelp表示:

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

  17. Hey! This is kind of off topic but I need some advice from an established blog.
    Is it very difficult to set up your own blog? I’m not very techincal but I can figure things out pretty fast.

    I’m thinking about creating my own but I’m not sure where
    to start. Do you have any ideas or suggestions? With
    thanks

  18. A person essentially assist to make significantly posts I’d state.
    That is the first time I frequented your web page and up to now?
    I amazed with the analysis you made to create this particular publish amazing.
    Great task!

  19. Marcelabelp表示:

    https://mexicoph24.life/# mexican online pharmacies prescription drugs

  20. omaslot表示:

    I do believe all of the concepts you have presented on your post.

    They’re really convincing and will definitely work.
    Nonetheless, the posts are very quick for novices. Could you please prolong them a little from subsequent time?
    Thanks for the post.

  21. 9 Things Your Parents Taught You About Cast Iron Multi Fuel Stoves 5kw Cast iron Multi fuel stove

  22. 15 Strange Hobbies That Will Make You More Successful At Double
    Glazing Repairs Leeds double Glazing door repairs

  23. 14 Questions You’re Afraid To Ask About Washer And Dryer
    Combination washing Machines energy efficient

  24. Marcelabelp表示:

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

  25. JustinCit表示:

    indian pharmacies safe http://indiaph24.store/# world pharmacy india
    best india pharmacy

  26. Link Building consists of inserting links from and to other web sites.
    A technique Link Building involves putting Inbound Links from a separate webpage, Two
    Means Link Constructing entails placing both Inbound and
    Outbound Links and Three Approach Hyperlink Building involves more than 2 websites in the process.
    These companies depend on the strategies utilized by the various search
    engines to search out the websites. All .yu websites that failed to transition have been deleted, including historic
    ones. In some jurisdictions, including the U.S., a separate search just isn’t performed, but reasonably
    search and examination are combined. Within the face of
    competitors confronted by businesses today, Web optimization Companies play important roles in guaranteeing high rankings for the web
    site of the business in well-liked search engines like google’ results.

    Based mostly on these methods, Search engine optimization Consultants make modifications in the web site
    so that they can be easily found by the Search Engine Spiders and Crawlers.
    High quality of life measuring instruments can even promote a destructive and pessimistic view for
    clinicians, patients, and households, particularly when used at baseline during the time
    of analysis. This caveat should be added or the dynamics of this might be
    taken into account when decoding and understanding the results from a top quality of life
    measuring instrument.

  27. What’s The Current Job Market For 7kw Multi Fuel Stove Professionals?
    7kw multi fuel stove

  28. Mercedes Keys表示:

    Is Your Company Responsible For The Keys For Mercedes Budget?
    12 Top Ways To Spend Your Money Mercedes Keys

  29. What’s The Job Market For Renault Clio Car Key Replacement Professionals Like?
    renault clio car key Replacement

  30. mciampa.com表示:

    Here is my webpage hookup page on facebook (mciampa.com)

發佈留言

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