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,233,120 Responses

  1. WilliamRhype表示:

    http://finasteride.store/# buy propecia without dr prescription

  2. The Reasons To Focus On Enhancing Upvc Doors Leeds composite door repairs leeds

  3. 10 Apps To Help You Manage Your Mesothelioma Lawyer Mesothelioma legal
    (http://www.healthndream.com)

  4. RandallTaulp表示:

    cost propecia without dr prescription: propecia otc – get cheap propecia pill

  5. The Most Underrated Companies To Monitor In The Foldable Flat Treadmill Industry Fold Flat treadmill

  6. WilliamRhype表示:

    http://ciprofloxacin.tech/# buy generic ciprofloxacin

  7. Vauxhall Key表示:

    Vauxhall Key Fob Tips To Relax Your Daily Lifethe One Vauxhall Key Fob Technique Every Person Needs To Learn Vauxhall Key

  8. lawsuits表示:

    Why Nobody Cares About Truck Crash Attorney lawsuits

  9. WilliamRhype表示:

    http://nolvadex.life/# buy tamoxifen

  10. Davidtes表示:

    cost of generic propecia tablets buying cheap propecia propecia no prescription

  11. 5 Killer Quora Answers On Double Glazed Doors Near Me double glazed wooden windows

  12. 10 Things Your Competitors Can Lean You On Lavazza Coffee Machine easy to clean coffee machines

  13. Davidtes表示:

    where can i order lisinopril online how much is lisinopril 40 mg can i order lisinopril online

  14. RandallTaulp表示:

    buy cipro cheap: ciprofloxacin 500mg buy online – buy cipro online

  15. See What Amazon Uk Online Shopping Clothes Tricks The
    Celebs Are Using amazon uk online shopping clothes

  16. What Is Designer Handbags Red? Heck What Exactly Is Designer Handbags Red?
    Famous Handbag Designers

  17. RandallTaulp表示:

    nolvadex 10mg: benefits of tamoxifen – is nolvadex legal

  18. Keygen表示:

    Hi, I think your website might be having browser compatibility issues.
    When I look at your blog in Opera, it looks fine but when opening
    in Internet Explorer, it has some overlapping. I just wanted
    to give you a quick heads up! Other then that, fantastic blog!

  19. The 10 Most Terrifying Things About List Of Online Shopping Sites In Uk list of online shopping Sites in uk

  20. RandallTaulp表示:

    buy cytotec over the counter: order cytotec online – Misoprostol 200 mg buy online

  21. I hаve ben browsing online more than three hours today, yet I never found any interdesting
    aticle liкe yours. It’s prfetty worth enouցh for me.
    Personally, if all site ownerѕ ɑnd bloɡgers made gоold content ass уou did, the internet wikl be a lot more useful than ever before.

    my web-site; sumber daya tambahan

  22. A Citroen C3 Key Fob Replacement Success Story You’ll Never Remember Replacement Key For Citroen C1

  23. 9 Lessons Your Parents Taught You About Online Famous Shopping Sites online famous shopping Sites (m.042-527-9574.1004114.Co.Kr)

  24. Vacuums robot表示:

    An In-Depth Look Into The Future: What Will The Best Shark Robot
    Vacuum Industry Look Like In 10 Years? Vacuums robot

  25. Arletha表示:

    10 Times You’ll Have To Learn About Designer Handbags Red designer bags; Arletha,

  26. Conrad表示:

    Nine Things That Your Parent Teach You About Treatment
    For Adult ADD treatment for adult add (Conrad)

  27. bdsm表示:

    I really like your blog.. very nice colors & theme.
    Did you create this website yourself or did you hire someone to do
    it for you? Plz reply as I’m looking to design my own blog and would like
    to know where u got this from. thanks

  28. The Most Convincing Proof That You Need Large American Fridge Freezers american Fridge freezer with Ice dispenser

發佈留言

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