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,365,873 Responses

  1. Five Essential Tools Everyone In The Best French Door Fridge Freezer Industry Should Be Utilizing french Door Fridge freezers uk

  2. В магазине сейфов предлагают сейф купить москва сейф купить

  3. The 10 Most Terrifying Things About Titration ADHD Meds titration adhd meds

  4. нарколог на дом вывод из запоя на дому нарколог на дом вывод из запоя на дому .

  5. May Covert表示:

    Say “Yes” To These 5 Bandar Online Togel Tips Togel4D

  6. Guide To Situs Toto Togel: The Intermediate Guide On Situs Toto Togel situs Toto

  7. I visited several web sites except the audio quality for audio songs present at this site is really superb.

  8. В магазине сейфов предлагают купить сейф сейф купить в москве

  9. Elisha Devito表示:

    What Is The Reason? Audi A5 Replacement Key Is Fast Becoming The Hot Trend For 2023 Replacement Audi A3 Key

  10. Eventually, this is your very own choice and it’s possibly a discussion you
    require 401k rollover to ira (https://awaregift.com/classificadoseanuncios/index.php?page=user&action=pub_profile&id=12692) have with your financial expert.

  11. Thalia Shupe表示:

    A New Trend In Wood Burning Stoves Uk best wood burning backpacking stove (Lou)

  12. This Is The History Of Electric Trikes In 10 Milestones
    Indoor Electric Mobility Scooter

  13. Robin表示:

    I every time spent my half an hour to read this blog’s articles
    everyday along with a cup of coffee.

  14. Magnificent beat ! I wish to apprentice while you amend your web site, how
    could i subscribe for a blog website? The account helped me a acceptable
    deal. I had been tiny bit acquainted of this your broadcast offered bright clear concept

  15. Donnie Soliz表示:

    11 Methods To Totally Defeat Your Car Lock Smith Car Key Mobile Locksmith

  16. 20 Inspiring Quotes About Affordable Bunk Beds For Kids bunk beds for kids with stairs

  17. Pam Morrison表示:

    10 Beautiful Graphics About Coffee Pods Machine coffee pods machines

  18. The Main Issue With Diagnosis For ADHD And How You Can Solve It who can diagnose Adhd In adults

  19. 15 Facts Your Boss Wished You’d Known About Biofuel Fire bio ethanol fire place – Alanna,

  20. Attractive element of content. I simply stumbled upon your weblog and in accession capital to claim that I
    acquire in fact loved account your weblog posts. Any way I will be subscribing to
    your feeds and even I success you access consistently fast.

  21. 5 Killer Quora Answers On Double Glazed Near Me Double Glazed Near Me

  22. The Top Companies Not To Be Watch In The Adhd Assessment In Adults Industry adhd assessments for Adults

  23. See What Doctor Windows Tricks The Celebs Are Utilizing doctor window (diggerslist.com)

  24. Good day! This post could not be written any better! Reading through this post reminds me
    of my good old room mate! He always kept talking about this.

    I will forward this write-up to him. Pretty sure he will have a good read.

    Many thanks for sharing!

  25. Jeremypag表示:

    buy cheap ozempic ozempic online buy ozempic

  26. Frankevild表示:

    https://ozempic.art/# buy cheap ozempic

  27. Tiffany Husk表示:

    Fastidious answer back in return of this issue with solid arguments and explaining the whole thing regarding that.

發佈留言

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