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,245,975 Responses

  1. Greetings I am so glad I found your blog, I really found you by error, while I was searching on Digg for something else, Nonetheless I am here now and would just like to say thanks a lot
    for a fantastic post and a all round interesting blog (I also love the theme/design), I don’t have time to read through it
    all at the minute but I have saved it and also added your RSS feeds, so when I have time I will be back to
    read a lot more, Please do keep up the fantastic work.

  2. BrandenSlomy表示:

    amoxicillin 250 mg: cheap amoxil – amoxicillin 500 mg cost

  3. Узнай все о операция по увеличению пениса цена операции по увеличению полового члена

  4. feet pics表示:

    Spot on with this write-up, I honestly believe that this site
    needs far more attention. I’ll probably be returning to read
    more, thanks for the info!

  5. certainly like your web site however youu ave to test tthe
    spelling onn quite a few of your posts. Many of them are rife with
    spelling problems and I in finding it very troublesome to infrm the truth however I’ll certainly come again again. https://storage.googleapis.com/g7a/Wordpress-website-development/WORDPRESS-SUPPORT-&-MAINTENANCE/WordPress-Speed-And-Performance-Optimization-Service.html

  6. pepek表示:

    I know this web site presents quality based articles and additional stuff,
    is there any other web page which gives these things in quality?

  7. situs penipu表示:

    Generally I do not learn article on blogs, but I would like to say that this write-up very compelled me to take a look at
    and do so! Your writing taste has been surprised me.
    Thank you, quite nice article.

  8. BrandenSlomy表示:

    priligy: cheap priligy – dapoxetine online

  9. Namun, sepak bola lebih populer hampijr diseluruh dunia,
    tidak sepeti permainan bola basket yang terkenal hanya
    dari di beberapa negara saja. Kamu bixa konsultasi dengan dokter jantung di Halodoc
    yang tersedia 24 jam kapan saja dan di mana saja.

  10. Heya i am for the first time here. I found this board and
    I find It truly useful & it helped me out much. I hope to give something back and help others like
    you helped me.

  11. Ghost carts表示:

    Thank you for sharing your info. I truly appreciate your efforts and I will be waiting for your next post thanks once again.

  12. Luke Burgess表示:

    Have you ever attempted utilizing hot water in your pressure washer? Does it actually make that much difference? pressure washing

  13. выведение из запоя воронеж стационар my.forum2.net/viewtopic.php?id=174 .

  14. link bokep表示:

    Hi, I read your blogs on a regular basis. Your humoristic style is witty, keep doing what you’re doing!

  15. BernardMop表示:

    antiplatelet drug PlavixClo Best Price buy clopidogrel online

  16. Узнай все о операция по увеличению члена цена удлинение полового члена цена

  17. Bitstream AI表示:

    That is really fascinating, You’re an excessively skilled blogger.
    I’ve joined your feed and look ahead to in quest of more of your excellent post.
    Additionally, I have shared your web site in my social networks

  18. Узнай все о увеличение размера полового члена увеличение полового члена хирургическим путем

  19. canna cavi表示:

    Appreciate the recommendation. Let me try it out.

  20. The aesthetic appeal of metal roofs can’t be denied—they add character to any home! Explore styles at metal roofers

發佈留言

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