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,228,456 Responses

  1. Ronaldpsymn表示:

    https://finasteride.store/# buying propecia for sale

  2. Αn outstanding share! I have just forwarded this onto a co-workeг who has been conducting a litle researdh on this.
    Αnd he in fact orɗered me dinner because I stumbled upon it
    for him… loⅼ. So let me reword this…. Thannks for the meal!!
    Butt yeah, thanks for spending the tije to discuss this issue here on your weƄ ρage. https://able.extralifestudios.com/wiki/index.php/Rakyat_Hutan_Menyejahterakan_Rp_50._000_Dalam_Undian_Ck4d_Link_Alternatif_Ck4d_Login_Ohio

  3. Davidtes表示:

    cipro pharmacy ciprofloxacin generic cipro ciprofloxacin

  4. Ronaldpsymn表示:

    https://ciprofloxacin.tech/# buy ciprofloxacin

  5. WilliamRhype表示:

    https://ciprofloxacin.tech/# ciprofloxacin

  6. Ronaldpsymn表示:

    http://lisinopril.network/# cost for 40 mg lisinopril

  7. WilliamRhype表示:

    http://cytotec.club/# buy cytotec pills

  8. It is important to raise a Cockatoo as a member of the family unit and not as the center of it.

  9. RandallTaulp表示:

    cheap propecia without dr prescription: cost of cheap propecia price – propecia pill

  10. Davidtes表示:

    buy cytotec in usa buy cytotec over the counter п»їcytotec pills online

  11. diazepam 10mg表示:

    Diazepam belongs to a group of drugs called benzodiazepines.

  12. Ronaldpsymn表示:

    http://ciprofloxacin.tech/# ciprofloxacin 500 mg tablet price

  13. The 10 Most Scariest Things About Ticktok
    Pornstars Ticktok Pornstars

  14. Davidtes表示:

    cytotec pills buy online buy cytotec online fast delivery cytotec abortion pill

  15. Guide To Method Titration: The Intermediate Guide Towards Method
    Titration method titration

  16. Покердом является официальным партнером Sochi Poker Festival.

  17. gay sex表示:

    Greetings! Very helpful advice in this particular article!
    It is the little changes that make the most significant changes.

    Many thanks for sharing!

  18. Ronaldpsymn表示:

    https://cytotec.club/# Cytotec 200mcg price

  19. What’s The Job Market For Adhd Adult Treatment
    Professionals? Adhd Adult Treatment

  20. WilliamRhype表示:

    http://lisinopril.network/# best generic lisinopril

  21. Ronaldpsymn表示:

    http://nolvadex.life/# tamoxifen 20 mg

  22. See What Examples Of Online Products Tricks The Celebs Are Making Use Of examples Of online Products

  23. Hello my family member! I want to say that this post is awesome, great written and come with almost all important infos.
    I’d like to look more posts like this .

    Also visit my page Lahore Escorts

  24. Davidtes表示:

    ciprofloxacin 500mg buy online cipro for sale cipro pharmacy

  25. WilliamRhype表示:

    http://lisinopril.network/# lisinopril 10 best price

  26. Hi there to all, since I am really eager of reading this
    weblog’s post to be updated on a regular basis.

    It consists of nice stuff.

  27. Итак, за 1-й депозит от рублей пользователю выдают 100 percent бонус.

  28. Adult表示:

    The Most Effective Assessments For Adhd Tricks To Make A Difference In Your
    Life Adult

  29. Ronaldpsymn表示:

    https://cytotec.club/# buy cytotec over the counter

  30. Davidtes表示:

    zestoretic 20 generic lisinopril online lisinopril without rx

發佈留言

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