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,909 Responses

  1. 10 Mobile Apps That Are The Best For Commercial Truck
    Accident Attorney truck wreck Attorney (https://www.metooo.es/)

  2. Noella表示:

    I do agree with all the concepts you have introduced to your post.
    They’re really convincing and can certainly work.
    Nonetheless, the posts are very brief for beginners. May just you please prolong them a little from subsequent time?
    Thank you for the post.

  3. How To Backup Microsoft Windows Xp Home Edition 에볼루션 도메인

  4. Therese表示:

    10 Facts About Accident Lawyer Brooklyn That Will Instantly Set
    You In A Positive Mood injury lawsuit (Therese)

  5. hiop表示:

    Night Spa hiop

  6. Stress Management And Rush Hour Traffic 하이오피사이트

  7. Informative article, exactly what I wanted to find.

  8. New Tips For Small Business Holiday Marketing 하이오피주소

  9. SyjjPaish表示:

    Many sites offer a good price of mesalamine immunosuppressant s online can be both safe and risky.

  10. Hallie Tucker表示:

    Well explained. Discover more at click here

  11. Janette表示:

    Article writing is also a fun, if you be acquainted with
    after that you can write otherwise it is difficult to write.

  12. Andre Rowe表示:

    My friend recommended Mt Roof Moss Removal

  13. 포천에도 돈 벌 곳은 있습니다. 포천 노래방알바로 연락 한 번 주세요

  14. Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell
    to her ear and screamed. There was a hermit crab inside and it pinched
    her ear. She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!

  15. JasonUnsoG表示:

    https://sweetbonanza25.com/# sweet bonanza demo oyna

  16. Многие пользователи предпочитают зеркала основного сайта Starda Casino, так как они обеспечивают более стабильный доступ к сети казино.

  17. Rosaline表示:

    At this moment I am ready to do my breakfast, after having my breakfast coming again to read additional news.

  18. Thanks for the valuable insights. More at kitchen remodeling company

  19. JasonUnsoG表示:

    http://casinositeleri25.com/# guvenilir casino siteleri

  20. FrankEmine表示:

    guvenilir casino siteleri: canl? casino siteleri – Casino Siteleri

  21. 요즘 가락쪽 침체기라고 하지만, 저희는 다릅니다. 가락동 노래방알바에서 확인 해보세요.

  22. Wallacefex表示:

    sweet bonanza oyna: sweet bonanza slot – sweet bonanza giris

  23. BradleyEdino表示:

    sweet bonanza yorumlar sweet bonanza slot sweet bonanza oyna

  24. Wallacefex表示:

    deneme bonusu veren siteler: deneme bonusu veren siteler – deneme bonusu veren yeni siteler

  25. Laura表示:

    9 Things Your Parents Taught You About Darling Hahns Macaw Darling hahns macaw (Laura)

  26. Kristi表示:

    Top Three Healthiest And Tastiest Protein Bars 오피 (Kristi)

  27. Gail表示:

    Good answers in return of this query with solid arguments and describing
    all concerning that.

  28. JasonUnsoG表示:

    http://slotsiteleri25.com/# slot oyunlar?

發佈留言

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