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,480,774 Responses

  1. AlbertDuM表示:

    Lisinopril 1st: buy Lisinopril 1st – buy Lisinopril online

  2. dede4d表示:

    Ꮃow, fɑntastic weblog ⅼayout! How long hɑve you eveг been bloɡging for?
    you make blⲟggіnbg look easy. The entire glance of your websitе is wonderful, let alone the contеnt material!

    Here is my Ƅlog; dede4d

  3. AlbertDuM表示:

    cheapest Lisinopril: cheapest Lisinopril – prinivil drug cost

  4. Hi i am kavin, its my first time to commenting anywhere, when i read this article i thought i could also
    make comment due to this sensible article.

  5. Nikefut表示:

    bitcoin dark web deep web drug markets tor markets links

  6. I’ve been browsing online more than 4 hours today, yet
    I never found any interesting article like yours. It
    is pretty worth enough for me. Personally, if all website owners and bloggers made good
    content as you did, the net will be a lot more useful than ever before.

  7. cuaca778表示:

    I am really glad to read this webpage posts which contains plenty of
    helpful facts, thanks for providing such information.

  8. BrandenSlomy表示:

    buy priligy: priligy max pharm – buy priligy max pharm

  9. Hi, its pleasant article on the topic of media print, we all know media is a
    great source of data.

  10. The mind Head injury attorney Florida attorneys at
    DE CARO & KAPLEN, LLP.

  11. AlbertDuM表示:

    buy cytotec over the counter: cyt premium – buy cytotec online fast delivery

  12. DarrenUnjuh表示:

    http://cytpremium.com/# buy cytotec in usa

  13. bokep terbaik sma toket gede menyala banget

  14. Piece oof wrіting ԝriting is alѕo a fun,
    if you be famіⅼiar with after that yoս can ѡrite otherwise it is
    difficult to write.

    mʏ website … dede4d (classicalmusicmp3freedownload.com)

  15. Mr Ring was born Jan 18, 1917, at Wilbur and remained there till his graduation from Wilbur High school .

  16. DarrenUnjuh表示:

    http://lisinopril1st.com/# buy Lisinopril online

  17. BrandenSlomy表示:

    cheap priligy: dapoxetine price – Priligy tablets

  18. DarrenUnjuh表示:

    https://lisinopril1st.com/# zestril tab 10mg

  19. It’s an amazing article in support of all the web viewers; they will take benefit from it I
    am sure.

  20. Your way of telling the whole thing in this paragraph is truly nice, every one be capable of effortlessly understand it, Thanks
    a lot https://www.provenexpert.com/nl-nl/pub018/

  21. You made some really good points there. I checked on the net for more information about the issue and found most people will
    go along with your views on this website.

  22. I do trust all of the concepts you’ve presented for your post.
    They are really convincing and will certainly work.
    Still, the posts are too quick for novices. May just you please prolong them
    a bit from next time? Thanks for the post.

發佈留言

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