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,228,671 Responses

  1. Derekfar表示:

    ultrabet guncel: ultrabet guncel – ultrabet giris

  2. Some Of The Most Ingenious Things That Are Happening With Asbestos Attorney In Virginia Mesothelioma Attorneys

  3. zapada01表示:

    I have been exploring for a bit for any high-quality articles or
    blog posts on this kind of house . Exploring in Yahoo I finally stumbled upon this web site.
    Studying this information So i am glad to convey that I’ve a very excellent uncanny feeling
    I came upon just what I needed. I so much indisputably will make certain to
    do not omit this website and give it a glance regularly.

    my site :: zapada01

  4. Kickass表示:

    Howdy very nice site!! Man .. Excellent .. Superb ..

    I’ll bookmark your site and take the feeds also?
    I’m happy to search out a lot of helpful information here in the post,
    we want develop more strategies on this regard,
    thanks for sharing. . . . . .

  5. jisuzm.com表示:

    What Is The Reason Best Pod Coffee Machine Is Right For
    You best value coffee machine; jisuzm.com,

  6. aiporn.mobi表示:

    Hey I know this is off topic but I was wondering if you knew of
    any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe
    you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy
    reading your blog and I look forward to your new updates.

  7. LarryCoell表示:

    deneme bonusu veren siteler mycbet.com deneme bonusu veren siteler yerliarama.org deneme bonusu veren yeni siteler

  8. LarryCoell表示:

    en kazancl? slot oyunlar? az parayla cok kazandiran slot oyunlar? en kazancl? slot oyunlar?

  9. Just want to say your article is as astonishing. The clearness for your put up is simply
    great and that i could suppose you’re knowledgeable on this subject.
    Fine with your permission let me to grasp your RSS
    feed to stay up to date with drawing close post.
    Thank you one million and please continue the gratifying work.

  10. What i do not understood is in reality how you are now not actually
    a lot more smartly-favored than you might be right now. You are
    so intelligent. You realize therefore significantly in relation to this matter, produced me personally consider it from a lot of various angles.
    Its like men and women aren’t involved except it is something to do with Girl gaga!

    Your own stuffs excellent. Always deal with it up!

  11. LarryCoell表示:

    matadorbet matadorbet.bid matadorbet.bid

  12. 15 Trends That Are Coming Up About Wall Mounted Electric Fireplaces white wall mounted electric Fireplaces

  13. Derekfar表示:

    matadorbet.bid: matadorbet giris – matadorbet.bid

  14. What’s up to all, how is the whole thing, I think every
    one is getting more from this site, and your views are fastidious for new viewers.

  15. Hi my family member! I wish to say that this article is amazing, great written and include almost all vital infos.
    I’d like to see more posts like this .

  16. 5 Killer Quora Answers On Cabin Bed Mid Sleeper With
    Storage cabin Bed mid Sleeper

  17. HassanbuifS表示:

    http://casinositeleri.win/# Casino Siteleri
    deneme bonusu veren siteler denemebonusu2026.com

  18. 11 “Faux Pas” That Are Actually OK To Create With Your Wheelchair Folding Ramp Van Ramps For Wheelchairs – Mobilemobilityscooters87210.Aioblogs.Com,

  19. I have learn some excellent stuff here. Definitely price bookmarking for revisiting.
    I wonder how a lot attempt you put to make the sort of fantastic
    informative site.

  20. Тут можно преобрести сейф для оружия цена сейф купить оружейный

  21. ClydePab表示:

    slot siteleri: en cok kazand?ran slot oyunlar? – en cok kazand?ran slot oyunlar?

  22. Virtual Mystery Boxes Isn’t As Difficult As You Think Mystery Box opening

  23. Rosaline表示:

    I blog often and I truly appreciate your content. This article has truly peaked
    my interest. I am going to book mark your blog and keep checking for new information about once
    a week. I subscribed to your Feed as well.

  24. Тут можно преобрести сейфы для ружья купить шкаф для ружья

  25. 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 hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m looking for something unique.
    P.S My apologies for getting off-topic but I had to ask!

發佈留言

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