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,466,827 Responses

  1. вывод из запоя дешево ростов-на-дону вывод из запоя дешево ростов-на-дону .

  2. Craig Bell表示:

    Medical spas have become my go-to for self-care days; they combine relaxation with results! Lotus Health and Aesthetics

  3. I know this if off topic but I’m looking into starting my
    own blog and was wondering what all is required 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 suggestions or advice would be greatly appreciated.
    Thanks

  4. toys for kids表示:

    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.

  5. Brian Young表示:

    I can not believe just how simple it was to lease a bounce home online! The procedure was smooth and simple with bounce house rental

  6. bokep 2023表示:

    While this was informative, I believe certain aspects were overlooked.

  7. Jared Pearson表示:

    Just booked my first consultation at a local medical spa custom facials

  8. Wallacefex表示:

    casino bahis siteleri: Casino Siteleri – Canl? Casino Siteleri

  9. Wallacefex表示:

    en guvenilir casino siteleri: Casino Siteleri – casino bahis siteleri

  10. 9 Signs That You’re The Buy Driver’s License B96 Without A
    Degree Online Expert KöPa Ett KöRkort

  11. Glenn表示:

    I appreciate, lead to I discovered exactly what I was
    having a look for. You have ended my four day long hunt!
    God Bless you man. Have a nice day. Bye

  12. 10 Great Ideas For Party And Wedding Entertainment 부달 (marvelvsdc.Faith)

  13. Helen表示:

    Hello there! This is my first comment here so I just wanted to give
    a quick shout out and tell you I genuinely enjoy reading your blog posts.
    Can you recommend any other blogs/websites/forums that deal with the same topics?
    Appreciate it!

  14. Hentai Hanime表示:

    Great information. Lucky me I found your blog by
    accident (stumbleupon). I have saved as a favorite for later!

  15. FrankEmine表示:

    slot oyunlar?: slot casino siteleri – en kazancl? slot oyunlar?

  16. FrankEmine表示:

    en guvenilir casino siteleri: en guvenilir casino siteleri – casino bahis siteleri

  17. Hi there just wanted to give you a quick heads
    up. The text in your post seem to be running off the screen in Opera.
    I’m not sure if this is a format issue or something to do with internet browser compatibility but I thought I’d post to let you know.

    The design and style look great though! Hope you get the problem solved soon. Many thanks

  18. Arron表示:

    Hey! Quick question that’s totally off topic. Do you
    know how to make your site mobile friendly?
    My web site looks weird when browsing from my apple iphone.
    I’m trying to find a template or plugin that might be able to correct this problem.
    If you have any recommendations, please share. Thank
    you!

  19. Thanks for breaking down the cost factors associated with different types of roofs; budgeting is so important—check out additional resources at roofing company

  20. JacobLoody表示:

    кухни от производителя — покупайте мебель напрямую от проверенного производителя.

  21. Erik Anderson表示:

    Thanks for the clear advice. More at dump trailers for sale near me

  22. Millie Lane表示:

    This was a wonderful guide. Check out رنگ ضد حریق for more

  23. 14 Creative Ways To Spend On Leftover Chest Freezer Near Me Budget
    Electriq chest Freezer

  24. FrankEmine表示:

    Canl? Casino Siteleri: canl? casino siteleri – en guvenilir casino siteleri

  25. Janelle表示:

    great submit, very informative. I’m wondering why the other specialists of this sector don’t understand this.
    You should proceed your writing. I’m confident,
    you’ve a huge readers’ base already!

  26. FrankEmine表示:

    gГјvenilir kumar siteleri: canl? casino siteleri – Casino Siteleri

  27. BradleyEdino表示:

    Casino Siteleri Casino Siteleri Deneme Bonusu Veren Siteler

發佈留言

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