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,477,254 Responses

  1. UG8表示:

    Hello to all, how is everything, I think every one is getting more from this
    site, and your views are fastidious for new viewers.

  2. Charlesbot表示:

    http://fastpillsformen.com/# viagra without prescription

  3. Telegram 係一款流行嘅即時通訊應用,紙飛機官網提供中文版本,用戶可以点击 飛機下載 獲取。呢個平台唔單止有電腦桌面版,仲提供適用於蘋果手機同安卓設備嘅 APK 版本。

  4. Idesys.co.kr表示:

    What’s up colleagues, its impressive piece of writing about teachingand fully defined, keep it
    up all the time. http://Idesys.co.kr/bbs/board.php?bo_table=free&wr_id=314623

  5. thietbibmc.vn表示:

    Hi, There’s no doubt that your site could be having browser compatibility issues.

    Whenever I take a look at your blog in Safari, it looks fine however when opening in I.E., it’s got some overlapping issues.

    I merely wanted to give you a quick heads up! Other than that, excellent blog!

  6. bbarlock.com表示:

    Wondeгful blog! Do you have any tiρs for aspiring writers?
    I’m planning to stаrt my own website soon butt Ι’m a little lost on everything.
    Would you avise starting with a free platform like Wοrdpress or ɡo fοгr a paijd option? There are so many choices out thdre that I’m completely overwhelmed ..
    Any tips? Apporeciate it!

    Also isit my web siye … bandung car rental (bbarlock.com)

  7. The 12 Most Obnoxious Types Of Accounts You Follow On Twitter Asbestos Lawyers

  8. BrettTIT表示:

    Generic Cialis price: Tadalafil price – п»їcialis generic

  9. Great insights! Find more at residential scaffolding

  10. 광주유흥表示:

    Planning A Stag And Doe Party On An Affordable 광주유흥

  11. Charlesbot表示:

    https://maxpillsformen.com/# Buy Tadalafil 10mg

  12. ProNerve6表示:

    Woah! I’m really enjoying the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s challenging to get that “perfect balance”
    between usability and visual appeal. I must say you have
    done a awesome job with this. Also, the blog loads super quick for me on Firefox.
    Excellent Blog!

  13. Max Snyder表示:

    Najlepszy blog informacyjny i tematyczny to świetne miejsce na zdobycie ciekawych informacji na różnorodne tematy. Dzięki regularnym aktualizacjom można być na bieżąco z nowinkami indeks

  14. 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트
    카지노사이트 카지노사이트

  15. 테라피表示:

    Few Attractive Lounges To Hold Around With Friends 테라피

  16. Diplomi_lupt表示:

    продам диплом о высшем образовании продам диплом о высшем образовании .

  17. Barrybib表示:

    Viagra online price: FastPillsForMen – sildenafil over the counter

  18. Gabriella表示:

    The Unspoken Secrets Of Best SEO Software UK professional seo tool, Gabriella,

  19. Jose Dean表示:

    Appreciate the detailed insights. For more, visit عمل prk

  20. BrettTIT表示:

    Cheap Cialis: Generic Cialis without a doctor prescription – Buy Tadalafil 20mg

  21. The Review of Economic Studies.

  22. Live 2 B Healthy
    Bloomington, Minneapolis 55402, United States
    16124200445
    Mental health and wellness for older adults

  23. I am not sure where you’re getting your information, but good topic.
    I needs to spend some time learning more or understanding more.
    Thanks for magnificent info I was looking for this information for my mission.

  24. Charlesbot表示:

    http://maxpillsformen.com/# buy cialis pill

  25. Donaldabula表示:

    Generic Tadalafil 20mg price MaxPillsForMen Cialis 20mg price in USA

發佈留言

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