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,587,738 Responses

  1. MichaelGeony表示:

    cheapest pharmacy for prescriptions http://familypharmacy.company/# Online pharmacy USA

  2. Porterfax表示:

    Mega India Pharm reputable indian pharmacies MegaIndiaPharm

  3. Thanks for the auspicious writeup. It in truth used to be a amusement account it.
    Look complex to far added agreeable from you! However,
    how can we be in contact?

  4. MichaelGeony表示:

    canadian pharmacy no prescription needed https://xxlmexicanpharm.com/# xxl mexican pharm

  5. SolarShop表示:

    My programmer is trying to persuade 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 Movable-type on a variety
    of websites for about a year and am worried about switching to another platform.

    I have heard great things about blogengine.net.
    Is there a way I can import all my wordpress posts into it?
    Any kind of help would be greatly appreciated!

  6. MichaelGeony表示:

    us pharmacy no prescription http://discountdrugmart.pro/# discount drug pharmacy

  7. MarkWat表示:

    Приветствую. Подскажите, где почитатьразные блоги о займах? Сейчас читаю – centrzaymov19.ru

  8. Davidmoits表示:

    indian pharmacy paypal: MegaIndiaPharm – MegaIndiaPharm

  9. Dannyfut表示:

    darknet drug links https://github.com/darknetmarketslinks/darknetmarketlinks – dark web access the dark internet

  10. JAVA189 LOGIN表示:

    Awesome site you have here but I was curious if you knew of any forums that cover the
    same topics discussed here? I’d really love to be a part of online community where
    I can get opinions from other knowledgeable individuals that share the same interest.
    If you have any recommendations, please let me know. Thanks!

  11. Davidmoits表示:

    easy canadian pharm: easy canadian pharm – easy canadian pharm

  12. Ten Things You Learned In Kindergarden Which Will
    Aid You In Obtaining Buy A1 And A2 Driving License Online
    köp Sverige körkort

  13. Hi! Iknow this iis kinda off topikc but I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest authoring
    a blog post or vice-versa? My website gods over a lot of
    the same subjects as yours and I believe wee could greatly benefit from each other.
    If you happen to be interested feel free to send
    me an email. I loo forward to hearing from you! Awesome
    blog by the way!

    Look at my webpage … izmir nişan mekanı

  14. Window Seal Repair: What’s No One Is Talking About window sealed unit replacement

  15. Dannyfut表示:

    free dark web https://github.com/darknetmarketslinks/darknetmarketlinks – dark markets 2025 darknet drug links

  16. Dannyfut表示:

    dark web links https://github.com/darknetmarketslinks/darknetmarketlinks – black internet deep web drug markets

  17. The Reasons Buy A Category A Driving License Is More Difficult Than You Think Kup Prawo jazdy W Polsce

  18. MichaelGeony表示:

    online pharmacy prescription https://easycanadianpharm.com/# canadian pharmacy prices

  19. MichaelGeony表示:

    canadian pharmacy coupon http://xxlmexicanpharm.com/# mexican drugstore online

  20. I am really impressed with your writing talents as well as
    with the layout to your blog. Is this a paid subject matter or did you customize it your self?

    Either way keep up the nice high quality writing, it is uncommon to see a great
    weblog like this one these days..

  21. Rodneytus表示:

    tor markets 2025 https://github.com/darkwebmarketslinks/darkwebmarkets – dark market url dark web drug marketplace

  22. I have read so many articles regarding the blogger lovers but this post is genuinely a good post, keep it up.

  23. Charleskepsy表示:

    no prescription needed pharmacy http://megaindiapharm.com/# MegaIndiaPharm

發佈留言

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