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,516,624 Responses

  1. WilsonVah表示:

    trouver un mГ©dicament en pharmacie: cialis prix – pharmacie en ligne avec ordonnance

  2. Richardprurf表示:

    https://tadalafilmeilleurprix.com/# acheter mГ©dicament en ligne sans ordonnance
    pharmacie en ligne sans ordonnance

  3. WilsonVah表示:

    п»їpharmacie en ligne france: Pharmacies en ligne certifiees – pharmacie en ligne france pas cher

  4. Jacobbep表示:

    Pharmacie en ligne livraison Europe http://tadalafilmeilleurprix.com/# pharmacie en ligne france livraison belgique

  5. Land 3 bonus offer signs to activate the Reward Perk Game.

    my site – online slots nz and online pokies nz

  6. In this instance, the life insurance company’s money.

    Also visit my blog post – ai detector for essays

  7. Perryzoozy表示:

    Achat mГ©dicament en ligne fiable cialis sans ordonnance pharmacie en ligne avec ordonnance

  8. Way cool! Some extremely valid points! I appreciate you penning this post plus
    the rest of the site what is a sexual assault advocate
    really good.

  9. In this case, the life insurance firm’s money.

    Also visit my site … ai detector turnitin free online

  10. Fantastic beat ! I wish to apprentice at the same time
    as you amend your website, how can i subscribe for a weblog web site?
    The account aided simple assault lawyers near me a appropriate deal.

    I had been a little bit acquainted of this your broadcast offered brilliant transparent idea.

  11. Somebody essentially help to make seriously posts I’d state.
    That is the first time I frequented your web page and to this point?

    I amazed with the analysis you made to make this particular post amazing.
    Excellent job!

    My website sexual assault lawyers philadelphia

  12. The most effective CBD items are made from high-grade hemp cbd oil for cats.

  13. Authentic HGH Benefits (NewULife Hgh Gel Not Checked Yet).

    my webpage nagad88 online casino Bangladesh login

  14. An artificial USP human development hormone (somatropin).

    My blog post :: miami corporate law

  15. Thank you, I’ve just been searching for information approximately this
    subject for ages and yours is the greatest I’ve found out so far.
    However, what concerning the conclusion? Are you positive about the source?

    Feel free to surf to my website sexual assault lawyers in charlotte nc

發佈留言

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