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,237,466 Responses

  1. ivimall.com表示:

    7 Secrets About Motor Vehicle Case That Nobody Will Share With
    You motor vehicle accident lawsuits (ivimall.com)

  2. 10 Unexpected Double Glazing Windows Leeds Tips double glazed Doors repairs

  3. I do not know whether it’s just me or if perhaps everyone else experiencing problems with your
    website. It appears like some of the text in your
    content are running off the screen. Can somebody else please comment
    and let me know if this is happening to them too? This may be a problem with my browser because I’ve had this happen previously.
    Many thanks

  4. Maira表示:

    The Greatest Sources Of Inspiration Of London Online Clothing Shopping Sites Gold Filled
    Men’s Necklace (Maira)

  5. 14 Cartoons On Examples Of Online Shopping Which Will Brighten Your Day Sylvania Two-Pack Bulbs

  6. 12 Facts About Special Slots To Bring You Up To Speed The Cooler Water Cooler Casino Slot Games

  7. efficient表示:

    Are You In Search Of Inspiration? Check Out Wash Machine And Dryer Combo efficient

  8. Aracely表示:

    Thanks a lot for sharing this with all of us you actually understand what you’re talking
    about! Bookmarked. Kindly additionally talk over with my
    web site =). We will have a hyperlink trade agreement among us

  9. WilliamRhype表示:

    http://finasteride.store/# cheap propecia tablets

  10. How You Can Use A Weekly Double Glazing Repairs Near
    Me Project Can Change Your Life Double glazing Replacement

  11. Good day! This is my 1st comment here so I just wanted to give a
    quick shout out and say I really enjoy reading
    through your posts. Can you recommend any other blogs/websites/forums that cover the same subjects?
    Thanks a ton!

  12. Search Engine Optimisation Agency: 11 Thing That You’re Failing To Do seo Agency London

  13. rutelochki.ru表示:

    10 Things You Learned In Kindergarden To Help You Get Side By
    Side Fridge Freezer Samsung Highest rated side By side refrigerator
    rutelochki.ru,

  14. WilliamRhype表示:

    http://finasteride.store/# order propecia no prescription

  15. RandallTaulp表示:

    cytotec pills online: cytotec pills online – buy cytotec over the counter

  16. Ronaldpsymn表示:

    https://lisinopril.network/# buy lisinopril 20 mg online uk

  17. Davidtes表示:

    does tamoxifen cause weight loss nolvadex vs clomid tamoxifen hip pain

  18. Right here is the perfect site for everyone who would like to find out about
    this topic. You know a whole lot its almost hard to argue with you (not that I actually will need to…HaHa).
    You definitely put a new spin on a subject that’s been discussed
    for a long time. Excellent stuff, just excellent!

  19. What’s The Current Job Market For Upvc Window Handle Replacement Professionals?
    window handle replacement – http://www.Huenhue.net

  20. Coin Auctions表示:

    This blog was… how do I say it? Relevant!!
    Finally I have found something that helped me.
    Thanks a lot!

    Here is my blog post :: Coin Auctions

  21. Coin Auctions表示:

    Way cool! Some extremely valid points! I appreciate you writing this write-up plus the rest of the site is extremely good.

    Feel free to visit my web blog … Coin Auctions

  22. 7 Effective Tips To Make The Maximum Use Of Your Avon Shop Online avon on (littleyaksa.Yodev.net)

  23. Klara表示:

    Five Lessons You Can Learn From Cost Of Replacement Windows
    Uk window replacements (Klara)

  24. installed表示:

    10 Things People Get Wrong About Double Glazing Repairs Near Me installed

  25. See What Veterans Disability Lawyer Tricks The Celebs Are Using Veterans Disability Lawyer

  26. Howdy! I could have sworn I’ve visited this website before but after looking at a few of
    the articles I realized it’s new to me. Regardless,
    I’m certainly pleased I discovered it and I’ll be book-marking
    it and checking back often!

  27. mojok34表示:

    Nice post. I learn something totally new and challenging on blogs I stumbleupon on a daily basis.
    It’s always useful to read through content from other authors and use a
    little something from other sites.

  28. How To Tell If You’re At The Right Level For Mini
    Cooper Replacement Keys mini key replacement

  29. Nine Things That Your Parent Teach You About Small Business SEO Services business Seo services

發佈留言

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