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,377,093 Responses

  1. RobertJar表示:

    buying generic clomid without a prescription: clomidonpharm – how can i get clomid price

  2. MiguelUnind表示:

    https://prednibest.com/# buy prednisone no prescription

  3. RobertJar表示:

    purchase amoxicillin online without prescription: Amox Star – amoxicillin order online

  4. RobertJar表示:

    where can i buy amoxicillin over the counter uk: amoxicillin 30 capsules price – can you purchase amoxicillin online

  5. Definitely believe that which you said. Your favorite reason appeared
    to be on the internet the easiest thing to be aware of.
    I say to you, I certainly get annoyed while people consider
    worries that they plainly don’t know about.
    You managed to hit the nail upon the top as well as defined out
    the whole thing without having side-effects , people can take a signal.
    Will likely be back to get more. Thanks

  6. Nikefut表示:

    drug markets dark web darknet seiten darknet drugs

  7. DanielAbice表示:

    https://clomidonpharm.com/# can i order generic clomid tablets

  8. SamuelBix表示:

    where can i buy cipro online: CiPharmDelivery – where to buy cipro online

  9. boneksport表示:

    Hi mates, how is all, and what you desire to say
    regarding this article, in my view its actually awesome designed
    for me.

  10. карниз с электроприводом для штор купить карниз с электроприводом для штор купить .

  11. H2Kelim.com表示:

    Asking questions are truly fastidious thing if
    you are not understanding anything completely, but this post presents good understanding yet. http://h2Kelim.com/bbs/board.php?bo_table=free&wr_id=165630

  12. ThelmaFange表示:

    On our website you will find unique collection rare coins from the whole planet. We offer both modern and historical specimens that will become real finds for collectors.
    For more information, follow the link монета 10000 рублей 2024
    Our team of experts carefully selects each coin, ensuring authenticity.

  13. MiguelUnind表示:

    https://clomidonpharm.com/# where can i buy cheap clomid without prescription

  14. DanielAbice表示:

    https://prednibest.com/# prednisone 10 mg online

  15. You’ll Never Guess This Tan Sectional Sofa’s Tricks Tan Sectional Sofa

  16. The Reasons To Work With This Car Key Replacement Near Me Emergency Car Key Replacement Near Me

  17. анонимный чат для вирта анонимный чат для вирта .

  18. Nicely put. With thanks!

  19. SamuelBix表示:

    can i purchase generic clomid without prescription: can you get cheap clomid without insurance – cost generic clomid no prescription

  20. resources表示:

    I seriously love your site.. Very nice colors & theme. Did you create this amazing site yourself?
    Please reply back as I’m hoping to create my own personal
    blog and would like to find out where you got this from or
    just what the theme is called. Appreciate it!

  21. DanielAbice表示:

    https://cipharmdelivery.com/# buy cipro without rx

  22. Heya i’m for the first time here. I came across this board and I find It really
    useful & it helped me out a lot. I hope to give something back and help others
    like you aided me.

  23. RobertJar表示:

    generic prednisone cost: Predni Best – can you buy prednisone over the counter

  24. a While taking care oneself may seem indulgent remember prioritizing mental health requires proactive measures including engaging activities which replenish energy levels frequently lost during hectic schedules — find inspiration through best spas

  25. Whɑt’s ᥙp, I logg onn tto you neew stuuff daily. Youur story-telling styke iss witty, eep itt up!

    Loook into myy hompaɡe – پیشنهاد ویژه فقط امروز

  26. Hi would you mind letting me know which web host you’re using?
    I’ve loaded your blog in 3 different browsers and
    I must say this blog loads a lot faster then most. Can you recommend
    a good internet hosting provider at a fair price? Cheers, I appreciate it!

  1. 2024-12-21

    sildenafil walgreens

    sildenafil walgreens

發佈留言

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