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,522,268 Responses

  1. indiablo.ru表示:

    My page :: Daycare Near Me – Find The Best Daycares Near You [indiablo.ru]

  2. Felipa表示:

    You’ll Be Unable To Guess Travel System Prams’s Tricks travel
    system prams (Felipa)

  3. memek表示:

    This is a topic that’s near to my heart… Cheers! Exactly where are
    your contact details though?

  4. Pokemon Karte表示:

    Incredible points. Solid arguments. Keep up the great spirit.

  5. KevinSounc表示:

    https://slotsiteleri.bid/# deneme bonusu veren siteler

  6. The 10 Most Terrifying Things About Bi-Fold Door Repairs
    Near Me Bi-Fold Door Repairs Near Me

  7. Hi there! Do you use Twitter? I’d like to follow you if that would
    be okay. I’m absolutely enjoying your blog and look forward to new posts.

  8. dancelover.tv表示:

    How Biofuel Fire Has Become The Most Sought-After Trend
    In 2023 bio ethanol Fire basket (dancelover.tv)

  9. 7 Things About Mesothelioma You’ll Kick Yourself For Not Knowing Mesothelioma Legal Counsel

  10. JeffreyLak表示:

    Medicine facts provided. Access pill facts.
    tadacip 20 mg
    Recent medicine developments. Patient medicine resource.

  11. American Style Fridge Freezers With Water And Ice Dispenser Tools
    To Streamline Your Daily Life American Style Fridge Freezers With Water And Ice
    Dispenser Trick That Everybody Should Know american style fridge freezers with water and ice dispenser

  12. Please let me know if you’re looking for a article author for your site.

    You have some really good articles and I feel I would be
    a good asset. If you ever want to take some of the load off, I’d love to write some articles for your blog
    in exchange for a link back to mine. Please shoot me an e-mail if interested.
    Kudos!

  13. ufabet表示:

    I’d like to find out more? I’d love to find out some additional information.

    My web site; ufabet

  14. bbarlock.com表示:

    Why You Should Focus On The Improvement Of French Door Fridge And Freezer french door
    fridge without water dispenser (bbarlock.com)

  15. Outstanding quest there. What occurred after?
    Take care!

  16. 10 No-Fuss Methods For Figuring Out Your Private Online Psychiatrist private psychiatrist Edinburgh Cost

  17. It’s amazing designed for me to have a site, which is
    valuable in support of my know-how. thanks admin

  18. The 10 Scariest Things About Best Integrated Fridge Freezer 50/50 Split Frost Free Integrated Fridge Freezer 50/50 Split Frost Free

  19. Lawyer表示:

    Hello there! This is my 1st comment here so I just wanted to give a quick shout out and say I genuinely enjoy reading through
    your posts. Can you suggest any other blogs/websites/forums that cover the same topics?

    Thanks a lot!

  20. Darrelherse表示:

    2024 en iyi slot siteleri: deneme veren slot siteleri – casino slot siteleri

  21. The Myths And Facts Behind Twin Tree House Bunk Bed play Area bunk Beds

  22. Simply ɗesire to say yur article is aas astonisһing. The clarity in your
    post іss simply cool and i could assume you’re an еxpert on this subject.
    Fine with your permisѕion ⅼеt mme to grab үour feed to
    kеep սpdated with forthcomіng post. Thanks a miⅼlion and please continue the rewarding work.

    My blog; Áo dạ nữ dáng dài

  23. 7 Easy Tips For Totally Rocking Your Senseo Single Serve
    senseo coffee Maker

  24. 5 Laws That Will Help With The Double Pushchair From Birth
    Industry Double Stroller for twins

  25. Fantastic goods from you, man. I have understand your stuff previous to and you are just extremely
    great. I actually like what you’ve acquired here, certainly like
    what you are saying and the way in which you say it.
    You make it entertaining and you still care for to keep it wise.
    I can not wait to read much more from you.
    This is really a terrific website. https://Fbi.me/index.php?title=User:CasieG503178049

  26. Fantastic beat ! I wish to apprentice while you amend
    your web site, how can i subscribe for a blog website?
    The account aided me a acceptable deal. I had
    been tiny bit acquainted of this your broadcast offered bright clear concept

  27. How To Explain Treadmills To A Five-Year-Old Treadmills Folding

  28. Darrelherse表示:

    slot oyun siteleri: deneme bonusu veren siteler – slot siteleri bonus veren

發佈留言

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