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,247,190 Responses

  1. BrandenSlomy表示:

    buy dapoxetine online: Priligy tablets – buy dapoxetine online

  2. Your Family Will Thank You For Having This Four Wheel Rollator rollator Walker

  3. Thanks , I have just been searching for information approximately
    this subject for a long time and yours is the greatest I have found
    out till now. But, what concerning the conclusion? Are you certain about the source?

  4. Poker Tournament Tips 다바오4989 (dnhangwa2.Webmaker21.kr)

  5. AlbertDuM表示:

    п»їplavix generic: PlavixClo – generic plavix

  6. I am extremely impressed with your writing skills and also with the layout on your blog.
    Is this a paid theme or did you customize it yourself?
    Anyway keep up the excellent quality writing, it is rare to see a nice blog like this one these days.

  7. AlbertDuM表示:

    stromectol tablets uk: minocycline 100mg tablets – minocycline foam

  8. DarrenUnjuh表示:

    http://iverfast.com/# can you buy stromectol over the counter

  9. Jonathan Rose表示:

    I love that your site offers practical steps victims can take immediately after an accident—it’s so necessary!:  # # a n y K e y w o r d Louisville car accident lawyer

  10. BrandenSlomy表示:

    amoxicillin discount coupon: antibiotic amoxicillin – amoxicillin without rx

  11. Currently iit looks like Wordpreѕѕ is tһe t᧐p blogging platfom avɑilable rіght now.
    (from what I’ve read) Is tһat whɑt you are using on your
    blog?

    my page; dede4d (https://bbarlock.com)

  12. BernardMop表示:

    Lisinopril 1st buy Lisinopril 1st lisinopril1st

  13. I have read some good stuff here. Definitely value bookmarking for revisiting.
    I surprise how so much effort you set to create any such magnificent informative web site.

  14. Have you ever thought about publishing an ebook or guest authoring on other websites?
    I have a blog centered on the same information you discuss and would love to have you share some stories/information. I know my audience would
    value your work. If you’re even remotely interested, feel free to shoot me an e-mail.

  15. Www.Google.Mn表示:

    15 Interesting Facts About Pragmatic You’ve Never Seen 프라그마틱 무료슬롯 (http://Www.Google.Mn)

  16. AlbertDuM表示:

    buy plavix: Clopidogrel Best Prices – plavix medication

  17. mega at表示:

    id=”firstHeading” class=”firstHeading mw-first-heading”>Search results

    Help

    English

    Tools

    Tools
    move to sidebar hide

    Actions

    General

  18. Thank you for sharing your info. I truly appreciate your efforts and I will be waiting for
    your further post thank you once again.

  19. sell cocaine表示:

    What’s up every one, here every person is sharing these familiarity, so it’s good
    to read this website, and I used to pay a quick visit this weblog
    everyday.

  20. DarrenUnjuh表示:

    http://iverfast.com/# stromectol 12mg

  21. butt dildos表示:

    What Butt Plugs Shop Experts Want You To Learn butt dildos

  22. Essas estruturas são famosas por serem sondadas e exploradas por criminosos.

  23. My brother suggested I might like this website. He was entirely right.
    This put up truly made my day. You can not believe simply
    how so much time I had spent for this information! Thanks!

  24. Please let me know if you’re looking for a writer for your
    blog. You have some really great articles and I believe I would be
    a good asset. If you ever want to take some of the load off, I’d really like to write some content for your blog in exchange for a link back
    to mine. Please blast me an e-mail if interested.
    Kudos!

  25. BrandenSlomy表示:

    how to buy generic clomid: clomid – can i order cheap clomid without a prescription

發佈留言

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