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,145,766 Responses

  1. The Companies That Are The Least Well-Known To Monitor In The Espresso
    Machines Industry coffee espresso machines

  2. Gisele表示:

    20 Questions You Should ASK ABOUT Upvc Window Repairs Before Purchasing It upvc window repairs near me (Gisele)

  3. Guide To Double Glazing Near Me: The Intermediate Guide For Double Glazing Near Me Double Glazing
    near Me; https://goodman-Warming-2.mdwrite.net,

  4. scamming表示:

    Hello there, just became aware of your blog through Google,
    and found that it is really informative. I’m going to watch
    out for brussels. I’ll appreciate if you continue this in future.

    Lots of people will be benefited from your writing.
    Cheers!

  5. The 10 Scariest Things About Autolocksmith Near Me Autolocksmith Near Me

  6. Bonnie表示:

    14 Misconceptions Commonly Held About Cheap Online Electronics Shopping Uk online shop (Bonnie)

  7. How London Windows And Doors Became The Hottest Trend Of 2023 Sash Window Repairs East London

  8. See What Gotogel Link Alternatif Tricks The Celebs Are Making Use Of gotogel link alternatif

  9. JamesNow表示:

    Buy Levitra 20mg online: levitrav.store – Levitra generic best price

  10. Vimeo表示:

    10 Of The Top Mobile Apps To Use For Birth Defect Legal Vimeo

  11. JamesNow表示:

    sildenafil 50 mg price: Cheap Viagra 100mg – Sildenafil Citrate Tablets 100mg

  12. CharlesDog表示:

    https://cenforce.pro/# order cenforce

  13. Hi there, just became alert to your blog through Google, and found that it is really informative.
    I am gonna watch out for brussels. I’ll appreciate if you continue this in future.

    Many people will be benefited from your writing. Cheers!

  14. Seat Car key表示:

    Why We Our Love For Seat Leon Replacement Key (And You Should, Too!) Seat Car key

  15. CharlesDog表示:

    https://viagras.online/# sildenafil over the counter

  16. Hello There. I found your blog using msn. This is a really well written article.
    I’ll be sure to bookmark it and come back to read more of
    your useful info. Thanks for the post. I will definitely return.

  17. Are You Getting The Most Of Your Rabbit Vibrators For Sale?
    rabbit Vibrator near Me

  18. CharlesDog表示:

    http://levitrav.store/# Vardenafil buy online

  19. Tangela表示:

    10 Law Personal Injury Tricks All Experts Recommend personal injury lawyers atlanta (Tangela)

  20. Annie表示:

    10 Of The Top Facebook Pages Of All-Time About Walking Desk Treadmill folding treadmill desk – Annie

  21. Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate?

  22. Here you can read and see the worlds lastest news and
    trend.

  23. Sexy表示:

    Hurrah! After all I got a weblog from where I know how to genuinely obtain useful information regarding my study and knowledge.

  24. JamesNow表示:

    Kamagra 100mg price: kamagra pills – buy Kamagra

  25. ElmerWes表示:

    Sildenafil 100mg price Buy generic 100mg Viagra online п»їBuy generic 100mg Viagra online

  26. Everyone loves it whenever people get together and share
    opinions. Great site, continue the good work!

    Visit my web-site solar hot water

  27. CharlesDog表示:

    http://kamagra.win/# Kamagra 100mg price

  28. Everything You Need To Learn About Luton Windows
    Front Doors Luton

  29. Is Cerebral Palsy Lawyers The Most Effective Thing That Ever Was?
    cerebral Palsy attorney

發佈留言

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