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,156,060 Responses

  1. A片表示:

    A片

    Howdy would you mind stating which blog platform you’re using?
    I’m planning to start my own blog in the near future but I’m having a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your design seems different then most blogs and I’m
    looking for something unique. P.S Sorry for getting off-topic but I had to ask!

  2. Hello to every body, it’s my first visit of this website; this webpage contains
    remarkable and really excellent information designed for readers.

  3. Be On The Lookout For: How Semi Truck Legal Is
    Gaining Ground And What Can We Do About It semi truck accident Lawsuits

  4. 10 Things That Your Competitors Teach You About Case Opening Sites CSGO cs2 case opening

  5. Howdy! I know tһis is somewhat off topic Ƅut Iwass wondering whіch blog platform arre уоu using
    foг this website? I’m gettіng tired of WordPress becaᥙse Ӏ’ve had issues
    witth hackers and I’m ⅼooking аt alternatives fߋr anotһer platform.
    I wouⅼd be awesome іf уou cⲟuld point me in the direction oof ɑ gоod platform.

    Chrck ouut mʏ blog post :: {link slot gacor hari ini}

  6. hey表示:

    Do you have a spam issue on this website; I also am a blogger, and I was wondering your situation; we
    have created some nice methods and we are looking to exchange techniques with other
    folks, please shoot me an email if interested.

  7. Keep this going please, great job!

  8. Nelly表示:

    Best Single Travel Stroller Tools To Help You Manage Your Daily Lifethe One
    Best Single Travel Stroller Technique Every Person Needs To Know single travel stroller (Nelly)

  9. Hi there, I want to subscribe for this web site to get hottest
    updates, thus where can i do it please help.

    Also vsit my webpage; Aizen Power Reviews

  10. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each
    time a comment is added I get four emails with the same comment.
    Is there any way you can remove me from that service? Bless you!

  11. Honda crv key表示:

    Where Is Where Can I Get A Honda Key Cut Be 1 Year From In The Near Future?
    Honda crv key

  12. 9 . What Your Parents Teach You About Window Sash Replacement Window sash Replacement

  13. 20 Insightful Quotes On Upvc Windows Repair Repair Upvc Windows (https://Ai-Db.Science)

  14. Ηello to all, aas I am rеally eager օf reading this blog’ѕ post to
    be updated on a regular basis. Іt contɑins goⲟɗ material.

    Ηere is my web pаge :: {link slot gacor hari ini}

  15. Davidplosy表示:

    Discovered an article that will surely interest you Р I recommend checking it out https://krasnodarforum.ru/member.php?u=2812

  16. Robby表示:

    10 Things You Learned In Preschool That Will Help You With Truck
    Accident truck accident lawsuit – Robby

  17. 10 Private Psychiatrist Belfast Cost Tricks Experts Recommend Private Online Psychiatrist Uk – http://Www.Newhopebible.Net

  18. 5 Killer Quora Answers To Online Shopping Sites
    Uk Online shopping sites

  19. I pay a quick visit everyday a few web pages and sites to read posts, however this web
    site provides quality based articles.

  20. plumber表示:

    When I initially left a comment I appear to have clicked on the -Notify me when new comments are added-
    checkbox and from now on every time a comment is added I
    recieve 4 emails with the exact same comment.
    There has to be a means you can remove me from that service?
    Many thanks!

  21. Danieldob表示:

    Всем привет! Может кто знает, где почитатьразные статьи о недвижимости? Сейчас читаю https://tent44.ru

  22. Azkgc.com表示:

    When I initially left a comment I appear to have
    clicked on the -Notify me when new comments are added- checkbox and now whenever a comment is added I receive four emails with
    the exact same comment. Is there a means you can remove me from that service?
    Thanks a lot! http://azkgc.com/?document_srl=346179

  23. See What Anxiety Disorders Medicine Tricks The Celebs Are Making Use Of Anxiety Disorders Medicine

  24. ArnulfoReipt表示:

    Declan Rice https://declan-rice.prostoprosport-fr.com Footballeur anglais, milieu defensif du club d’Arsenal et de l’equipe nationale equipe d’Angleterre. Originaire de Kingston upon Thames, Declan Rice s’est entraine a l’academie de football de Chelsea des l’age de sept ans. En 2014, il devient joueur de l’academie de football de West Ham United.

  25. 20 Myths About Veterans Disability Attorney: Busted Veterans disability Lawsuits

發佈留言

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