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

  1. Williamagric表示:

    carprofen without vet prescription https://mexicanpharm24.cheap/# mexican mail order pharmacies

  2. I was very happy to discover this page. I need to to thank you for your time due to this wonderful read!!
    I definitely loved every little bit of it and i also have you book-marked to check out new stuff on your web site.

  3. Awesome issues here. I am very glad to see your post.
    Thank you so much and I’m looking forward
    to contact you. Will you please drop me a e-mail?

  4. LeroyBak表示:

    india online pharmacy India pharmacy international indian pharmacy paypal

  5. DevonPhack表示:

    http://indianpharm24.pro/# top online pharmacy india
    legal to buy prescription drugs from canada

  6. LeroyBak表示:

    reputable mexican pharmacies online Legit online Mexican pharmacy mexico drug stores pharmacies

  7. Medicines information leaflet. What side effects can this medication cause?
    lisinopril 5mg tablets
    All what you want to know about medicine. Get information now.

  8. BrandonSaisy表示:

    reputable mexican pharmacies online: mexico pharmacy cheap – best online pharmacies in mexico

  9. The 10 Most Terrifying Things About Key Programmers Key Programmer (https://Coral-Panda-Hvtdqq.Mystrikingly.Com/)

  10. Great website you have here but I was curious if you knew
    of any message boards that cover the same topics discussed in this article?
    I’d really love to be a part of online community where I can get opinions from other experienced people that share the
    same interest. If you have any recommendations, please let me know.
    Thanks a lot!

  11. DevonPhack表示:

    http://pharm24.pro/# ed doctors
    best ed solution

  12. LeroyBak表示:

    reputable mexican pharmacies online mexico pharmacy cheap medication from mexico pharmacy

  13. Williamagric表示:

    vacuum pumps for ed http://pharm24.pro/# best ed pills that work

  14. Jorgeagopy表示:

    medication from mexico pharmacy: mexican drugs – best online pharmacies in mexico

  15. DevonPhack表示:

    http://indianpharm24.pro/# best india pharmacy
    otc ed drugs

  16. Thankfulness to my father who shared with me regarding this website, this webpage is in fact amazing.

  17. Williamagric表示:

    over the counter ed medication https://mexicanpharm24.cheap/# medicine in mexico pharmacies

  18. Ten Ways To Build Your Car Key Programmer Near Me Empire fob key programming (ebooksworld.com.pl)

  19. bokep xnxx表示:

    What’s up, its fastidious paragraph regarding
    media print, we all be familiar with media is a wonderful source of information.

  20. LeroyBak表示:

    ed doctor cheap meds ed medicine online

  21. Lazrmvc表示:

    Покупка диплома о среднем полном образовании: как избежать мошенничества?

    physmathforum.flybb.ru/viewtopic.php?f=12&t=996

  22. DevonPhack表示:

    http://indianpharm24.pro/# п»їlegitimate online pharmacies india
    ed solutions

  23. DevonPhack表示:

    https://mexicanpharm24.cheap/# medication from mexico pharmacy
    canada ed drugs

  24. Guide To Car Key Immobiliser Repair: The Intermediate Guide On Car
    Key Immobiliser Repair car Key Immobiliser Repair

  25. LeroyBak表示:

    mexico drug stores pharmacies mexico pharmacy п»їbest mexican online pharmacies

發佈回覆給「DevonPhack」的留言 取消回覆

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