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,231,883 Responses

  1. Ronaldpsymn表示:

    http://nolvadex.life/# nolvadex for sale

  2. RandallTaulp表示:

    get cheap propecia no prescription: cost of propecia pill – get propecia online

  3. Wow that was strange. I just wrote an really long comment but after I clicked submit
    my comment didn’t appear. Grrrr… well I’m not writing all
    that over again. Regardless, just wanted to say wonderful blog!

  4. Davidtes表示:

    ciprofloxacin generic ciprofloxacin order online cipro pharmacy

  5. agenolx login表示:

    Great information. Lucky me I came across your site by accident (stumbleupon).
    I’ve book-marked it for later!

  6. WilliamRhype表示:

    http://finasteride.store/# cost of propecia price

  7. Best Automatic Folding Mobility Scooter Uk Tools To Streamline Your Daily Lifethe One Best Automatic Folding
    Mobility Scooter Uk Trick That Everybody Should Know best automatic folding mobility scooter uk

  8. replacing表示:

    10 Essentials On Replace A Lost Car Key You Didn’t Learn In School replacing

  9. lihat disini表示:

    Τhanks to my father who told me regarding this webpage,
    this weblog is actually awеsome.

    Feeⅼ freе to visіt my web-site; lihat disini

  10. Ten Startups That Will Revolutionize The Leeds Double Glazing Industry For The Better
    double glazed Door Repair Leeds

  11. Ronaldpsymn表示:

    http://ciprofloxacin.tech/# ciprofloxacin generic

  12. It’s Time To Increase Your Renault Clio Key Fob Options renault clio Key fob replacement

  13. I’ve beеn browsing onlinje moгe tһan 3 hours today,
    yet I neᴠer found any interesting article like yours.
    It’s pretty worth enough for me. Personally, if all web ownerѕ аnnd
    bloggers made goood content as you did, the іnternet will be mucһ more ᥙseful tһwn eνer before.

    Alsso visіt mʏ homepage; cheap Outboards f᧐r sale (http://hawkee.com)

  14. Could Sales Team Leadership Be The Key To
    2022’s Resolving? avon sales leadership

  15. RandallTaulp表示:

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

  16. bunk beds uk表示:

    The No. 1 Question Anyone Working In Bunk
    Bed For Adults Needs To Know How To Answer bunk beds uk

  17. Five Killer Quora Answers To Situs Alternatif Gotogel
    situs alternatif Gotogel

  18. RandallTaulp表示:

    hysterectomy after breast cancer tamoxifen: tamoxifen – tamoxifen mechanism of action

  19. 15 Presents For The Replace Lost Car Key Lover In Your Life
    How To Replace Lost Car Key

  20. Why Car Lock Repair Near Me Will Be Your Next Big Obsession Automobile Door Locks

  21. 10 Things That Your Family Taught You About Best Cockrings best Cockrings

  22. RandallTaulp表示:

    cipro for sale: buy cipro online – cipro online no prescription in the usa

  23. sex表示:

    Нello to every single οne, it’s reaⅼly a plеasant for me
    to pay a quick visit this weƄsite, it consіsts of helpful Іnformation. https://csirt.tanahbumbukab.go.id/singha/?instagram=palu4d

  24. RandallTaulp表示:

    buy lisinopril online no prescription india: lisinopril 2.5 mg buy online – buy lisinopril mexico

  25. Guide To Saab Replacement Keys Uk: The Intermediate Guide For Saab Replacement
    Keys Uk Saab Replacement keys uk

  26. 10 Things That Your Family Teach You About Double Glazing Repair
    Near Me double glazing repair near Me – http://www.cskfloor.com/gnuboard5/bbs/board.Php?bo_table=inquiry&Wr_id=44437,

  27. vidude.com表示:

    What’s up everyone, it’s my first pay a visit at this web page,
    and paragraph is really fruitful designed for
    me, keep up posting these content.

  28. The Most Effective Hand Bags Tommy Hilfiger Tips For Changing Your Life tommy hilfiger bag for Mens

  29. Guide To Mesothelioma Lawsuit: The Intermediate
    Guide For Mesothelioma Lawsuit Mesothelioma Law

發佈留言

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