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,154,246 Responses

  1. Hi there, I log on to your blog on a regular basis. Your writing style is witty, keep up the
    good work!

  2. You’ll Never Guess This Double Glaze Repair Near Me’s Tricks
    double glaze repair near me

  3. Guide To Double Glazing Window Repairs: The Intermediate Guide To
    Double Glazing Window Repairs Window Repairs, Maps.Google.Tg,

  4. Meds information leaflet. What side effects?
    cheap anastrozole without a prescription
    Actual information about medicine. Get now.

  5. escortexxx.ca表示:

    The Most Common Mistakes People Make With Semi
    Truck Legal semi truck accident lawsuit [escortexxx.ca]

  6. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://onkopaednki.de/tvqrns of type and scrambled it to make a type specimen book.

  7. 15 Of The Best Twitter Accounts To Discover U
    Shaped Sectional Leather small u shape sofa (whippet-insider.de)

  8. Window Repair表示:

    The 10 Scariest Things About Emergency Window Repair Window Repair

  9. The 10 Most Scariest Things About Online Store Uk Cheapest online store Uk cheapest

  10. Wonderful site you have here but I was curious about if you knew of any discussion boards that cover
    the same topics discussed here? I’d really love to be a part of group where I can get comments from
    other experienced people that share the same interest.

    If you have any recommendations, please let me
    know. Cheers!

  11. bk8vico表示:

    Admiring the dedication you put into your site and in depth information you provide.
    It’s great to come across a blog every once in a while
    that isn’t the same outdated rehashed information. Wonderful read!
    I’ve saved your site and I’m adding your RSS feeds to
    my Google account.

  12. Window Repair表示:

    What Window Glass Repair Near Me Could Be Your Next Big Obsession Window Repair

  13. Upvc Door Lock Replacement: What’s No One Is
    Talking About replacing lock on upvc door

  14. What’s The Job Market For Car Accident Attorney Professionals
    Like? car Accident law firm

  15. Danieldob表示:

    Приветствую. Может кто знает, где почитатьразные статьи о недвижимости? Сейчас читаю https://universal37.ru

  16. calcal.net表示:

    Double Glazing Cambridgeshire: What’s The Only Thing Nobody Is Discussing door Seal repair near me (calcal.net)

  17. The Comprehensive Guide To Double.Glazing Near Me Double Glazing Windows

  18. The Ultimate Cheat Sheet On Repair Double Glazed Windows
    double glazed window repairs near me

  19. hey表示:

    WOW just what I was searching for. Came here by searching for BOWYANGS

  20. Vimeo表示:

    Online Shopping Website In London’s History Of Online Shopping Website
    In London In 10 Milestones Vimeo

  21. A片表示:

    A片

    It’s genuinely very complex in this full of activity life to listen news on TV,
    thus I only use web for that purpose, and take the latest news.

  22. 15 Gifts For The Repairs To Upvc Windows Lover In Your Life window Repairs

  23. tanah 189表示:

    Great items from you, man. I have keep in mind your stuff prior to and you
    are simply extremely great. I really like what you’ve received right here,
    certainly like what you are saying and the way in which during which you are saying it.
    You are making it entertaining and you still care for to stay it sensible.
    I can’t wait to read far more from you. That is really a great website.

  24. Corner Chaise Tools To Make Your Everyday Lifethe Only Corner Chaise Trick
    That Should Be Used By Everyone Learn corner Chaise; vilhelmsen-ryan-2.technetbloggers.de,

  25. 10 Myths Your Boss Is Spreading Concerning Free Slots famous slots (https://Fushihara.hatenadiary.Jp)

  26. 10 Of The Top Facebook Pages Of All Time Best Slots hacksaw gaming slot machine game

發佈留言

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