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,363,274 Responses

  1. Situs Alternatif Gotogel Tools To Ease Your Daily Life Situs Alternatif Gotogel Technique
    Every Person Needs To Know situs alternatif gotogel – http://gwwa.yodev.net,

  2. Why Online Shopping Website In London Is Everywhere This Year 2’3″ x 15′ runner rug

  3. Derrick表示:

    7 Simple Tricks To Totally Doing The Double Glazing Firms Near Me double glazing repair
    (Derrick)

  4. Keys表示:

    All The Details Of Nissan Qashqai Key Replacement Dos And Don’ts Keys

  5. Amazing blog! Do you have any tips for aspiring writers?
    I’m hoping to start my own blog soon but I’m a little lost
    on everything. Would you suggest starting with
    a free platform like WordPress or go for a paid option?
    There are so many options out there that I’m completely confused ..
    Any ideas? Thank you!

  6. The Reasons Why Robot Vacuum And Mop Self Empty Is Everyone’s Passion In 2023 Best self emptying
    vacuum (https://cs-upgrade.top)

  7. in my area表示:

    20 Inspirational Quotes About Private ADHD Diagnosis UK Cost in my area

  8. The One Double Glazed Windows Near Me Trick Every Person Should Learn Double glazing Unit replacement

  9. 20 Reasons Why Lost Auto Key Replacement Will Never Be Forgotten cost
    of replacing lost car keys – hessellund-ahmed.mdwrite.net,

  10. Hello! Do you know if they make any plugins to safeguard against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?

  11. 9 . What Your Parents Teach You About New Key For Peugeot 207 new key for peugeot 207 (http://Extension.unimagdalena.edu.co)

  12. Why You Should Focus On Making Improvements Double Glazed Window Replacement
    Near Me Repair Double Glazing Window

  13. It’s very simple to find out any matter on web as compared to books, as
    I found this post at this web page.

  14. padmaschine表示:

    Hello, yeah this piece of writing is genuinely nice and
    I have learned lot of things from it regarding blogging.
    thanks.

  15. wildfire case表示:

    5 Common Phrases About CSGO Most Profitable Cases You Should Stay Clear Of wildfire case

  16. Dominic表示:

    Five Killer Quora Answers On Mesothelioma Lawyer mesothelioma law, Dominic,

  17. 10 Things That Your Family Taught You About Replace Window Handles replace Window Handles

  18. Case opening表示:

    7 Small Changes That Will Make A Huge Difference In Your CSGO Case
    Battle Case opening

  19. 10 Tips For Quickly Getting Lg Side By Side Fridge Freezer side
    by side Freezer (https://morphomics.science)

  20. rentaltanaman表示:

    Hello there, just became aware of your blog through Google,
    and found that it’s really informative. I am gonna watch out for brussels.
    I’ll be grateful if you continue this in future. Numerous people will be benefited from your writing.
    Cheers!

  21. You’ll Never Guess This Best Robot Vacuum With Self Empty’s Tricks best robot Vacuum with Self empty

  22. 10 Amazing Graphics About Double Glazing Firms Near Me Repairing double glazing Windows

  23. cheaper表示:

    Are You Getting The Most Value From Your Double Glazing Repairs London? cheaper

  24. What’s The Job Market For Double Glazed Window Repairs Professionals Like?
    double glazed window repairs, http://Www.healthndream.Com,

  25. What’s The Current Job Market For Double Glazed Window Repairs
    Professionals Like? Window Repairs

  26. 12 Statistics About Prostate Massagers Store To Make You Think Twice About The Cooler Water Cooler shop Prostate Massager

  27. I’m really enjoying the design and layout of your site.
    It’s a very easy on the eyes which makes it much more
    pleasant for me to come here and visit more
    often. Did you hire out a developer to create your theme?
    Excellent work!

  28. Privatehd.org表示:

    This History Behind Double Glazing Repair Luton Will Haunt You For The Rest
    Of Your Life! double glazing windows near me (Privatehd.org)

發佈留言

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