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,239,038 Responses

  1. insurance表示:

    insurance | Identity Theft Protection: Identity theft insurance is becoming essential as cybercrime rises. MAFA Insurance helps clients find policies that offer protection and peace of mind.

  2. Way cool! Some extremely valid points! I appreciate you writing this
    post plus the rest of the site is really good.

  3. Thanks for the good writeup. It in truth was once a amusement account it.
    Look advanced to far added agreeable from you!
    However, how can we keep in touch?

  4. KevinSounc表示:

    https://sweetbonanza.network/# sweet bonanza 90 tl

  5. Thanks for one’s marvelous posting! I definitely enjoyed reading it, you’re a great author.I will remember to bookmark your blog and will often come back from now on. I want to encourage you to ultimately
    continue your great posts, have a nice holiday weekend!

  6. Visit Website表示:

    There is definately a great deal to learn about
    this subject. I like all of the points you have made.

  7. 10 Quick Tips About Ghost Installations range rover vogue
    ghost installer (https://kisdiconference.kr/)

  8. Woah! I’m really loving the template/theme of this blog.
    It’s simple, yet effective. A lot of times it’s very difficult to get
    that “perfect balance” between user friendliness and visual appearance.

    I must say you’ve done a very good job with this.
    Additionally, the blog loads super fast for me on Firefox.
    Exceptional Blog!

  9. What’s Holding Back This Double Glazing Windows Bedford
    Industry? Sash Window repair bedfordshire

  10. 5 Killer Quora Answers On Uk Private Psychiatrist uk Private psychiatrist

  11. 5 Killer Quora Answers To Saab Ignition Key Replacement saab Ignition key replacement [http://www.sodam.shop/]

  12. paryaj lakay表示:

    Tod Cifuentes is what people call him but he never really liked that name.
    Doing interior design is something he certainly not give moving
    upward. For years I have been living in North Carolina and
    I’ve everything we need correct here. For
    years I’ve been working as the computer operator but
    I’ve already went for another unique.

  13. You’ll Never Guess This Upvc Double Glazed Windows’s Benefits Upvc Double Glazed Windows

  14. Meds information leaflet. What side effects can this medication cause?
    how to buy aldactone price
    Best news about medication. Get information here.

  15. Why You’re Failing At ADHD Adult Test Adult Adhd Testing

  16. KevinSounc表示:

    https://sweetbonanza.network/# sweet bonanza 90 tl

  17. This article is really a pleasant one it helps new internet viewers, who are wishing for blogging.

  18. 5 Killer Quora Answers On ADHD Diagnose who can Diagnose add
    In adults – https://posnara.com/,

  19. KevinSounc表示:

    https://slotsiteleri.bid/# casino slot siteleri

  20. The Three Greatest Moments In Hyundai I30 Key Replacement History Hyundai Key replacement

  21. Ecellent write-սp. I definitely аppreciate tһis site.
    Ƭhanks!

    Ƭake a lo᧐k at mү blog … PokemonToto Togel

  22. I am really impressed together with your writing abilities and
    also with the structure on your weblog. Is this a paid subject
    matter or did you customize it yourself? Either way keep up
    the excellent quality writing, it is uncommon to
    look a nice blog like this one these days..

  23. Darrelherse表示:

    en cok kazandiran slot siteleri: slot siteleri bonus veren – guvenilir slot siteleri 2024

  24. I don’t know whether it’s just me or if everybody else experiencing
    problems with your website. It appears as though some of the written text on your posts are running off the
    screen. Can someone else please provide feedback and let me know if this
    is happening to them too? This might be a issue with my internet browser because I’ve had this happen before.

    Thanks

  25. Saxenda表示:

    I love reading through a post that can make people think.
    Also, thanks for allowing for me to comment!

  26. Darrelherse表示:

    deneme bonusu veren siteler: slot bahis siteleri – slot siteleri guvenilir

  27. KevinSounc表示:

    http://slotsiteleri.bid/# guvenilir slot siteleri

  28. KevinSounc表示:

    https://sweetbonanza.network/# guncel sweet bonanza

發佈留言

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