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,369,098 Responses

  1. Stephengen表示:

    http://clssansordonnance.icu/# pharmacie en ligne france pas cher

  2. полноростовой турникет купить http://for-gate.ru/ .

  3. Stephengen表示:

    https://vgrsansordonnance.com/# Viagra gГ©nГ©rique sans ordonnance en pharmacie

  4. This paragraph provides clear idea designed for the new people of blogging, that actually how to do blogging and site-building.

  5. I got this web site from my buddy who told me
    regarding this site and at the moment this time I am visiting this web site and reading very informative articles or reviews at
    this time.

  6. Charlesrab表示:

    pharmacie en ligne Cialis generique prix pharmacie en ligne france livraison internationale

  7. Uazrmva表示:

    Официальная покупка диплома вуза с сокращенной программой в Москве

    diplatt.ru/kak-bezopasno-i-legalno-kupit-diplom-v-RF

  8. This Story Behind Tree House Bunk Bed Can Haunt You Forever!
    Kids Treehouse Bed

  9. Stephengen表示:

    https://vgrsansordonnance.com/# Viagra homme sans ordonnance belgique

  10. Previous efficiency is no sign or warranty of future performance
    or returns.

    Also visit my homepage :: magic mushroom cafe

  11. Stephengen表示:

    http://vgrsansordonnance.com/# Viagra homme sans prescription

  12. Your means of explaining the whole thing in this article is really nice, all be capable of simply be aware of it, Thanks
    a lot https://boutiquelemarie.com/costume-du-temoin/

  13. My coder is trying to convince me to move to .net
    from PHP. I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using WordPress on various websites for about a year and am
    nervous about switching to another platform. I have heard great things about blogengine.net.
    Is there a way I can transfer all my wordpress posts into it?
    Any kind of help would be really appreciated!

  14. Stephengen表示:

    http://clssansordonnance.icu/# pharmacies en ligne certifiГ©es

  15. Mckenzie表示:

    I read this paragraph completely regarding the comparison of latest
    and earlier technologies, it’s awesome article.

  16. Charlesrab表示:

    Pharmacie Internationale en ligne cialis prix vente de mГ©dicament en ligne

  17. fundraising表示:

    I know this if off topic but I’m looking into starting
    my own weblog and was curious what all is needed to get setup?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very internet savvy so I’m not 100%
    certain. Any recommendations or advice would be greatly appreciated.
    Thank you

  18. You actually make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand.
    It seems too complicated and very broad for me. I am looking forward for
    your next post, I will try to get the hang of it!

  19. We are a group of volunteers and starting a new scheme in our
    community. Your web site offered us with valuable information to work on.
    You have done a formidable job and our whole community will be grateful to you.

  20. wink表示:

    Hey! I’m at work surfing around your blog from my new iphone 3gs!
    Just wanted to say I love reading through your blog and look forward to all your posts!
    Carry on the fantastic work!

  21. We are a group of volunteers and opening a new scheme in our community.
    Your web site provided us with valuable info to work
    on. You have done an impressive job and our entire community will be grateful to you.

  22. Charlesrab表示:

    Viagra vente libre allemagne Acheter du Viagra sans ordonnance Quand une femme prend du Viagra homme

  23. What Testing For ADHD In Adults Experts Would Like You To Be Educated test For Adhd in adults

  24. Teknoloji表示:

    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 really like to write
    some content for your blog in exchange for a link back to mine.
    Please send me an email if interested. Many thanks!

  25. Cecila表示:

    Five Killer Quora Answers To Situs Terpercaya situs terpercaya (Cecila)

  26. HeathZex表示:

    п»їpharmacie en ligne france: cialis sans ordonnance – pharmacie en ligne livraison europe

  27. Charlesrab表示:

    pharmacies en ligne certifiГ©es cialis sans ordonnance Pharmacie Internationale en ligne

發佈留言

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