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,479,039 Responses

  1. This was quite informative. More at residential scaffolding

  2. This post makes me excited about improving my l Helpful hints

  3. Lucinda Reed表示:

    I found this very interesting. Check out moving company near me for more

  4. срочное оформление загранпаспорта срочное оформление загранпаспорта .

  5. Barrybib表示:

    discount ed pills: FastPillsEasy – ed online pharmacy

  6. Donaldabula表示:

    cheap ed meds online FastPillsEasy where can i get ed pills

  7. Lura Steele表示:

    I am glad we used them as our construction company, they were able to get the job done as well as well as stay on budget. Professional outfit, the metal studs and sheetrock came out great for the office area Clifton

  8. Aw, this was an incredibly good post. Taking the time and actual effort to produce a superb article… but what can I say… I procrastinate a whole lot and don’t seem to get anything
    done.

  9. Barrybib表示:

    sildenafil online: FastPillsForMen.com – over the counter sildenafil

  10. Donaldabula表示:

    Generic Tadalafil 20mg price buy cialis online Cheap Cialis

  11. сколько стоит сделать загранпаспорт http://www.cinemania.forum24.ru/?1-15-0-00001690-000-0-0-1735288325 .

  12. Medicine information leaflet. Effects of Drug Abuse.
    brand name synthroid price
    Best what you want to know about meds. Read here.

  13. 카지노사이트 카지노사이트 카지노사이트 카지노사이트
    카지노사이트 카지노사이트 카지노사이트

  14. Hi there friends, how is all, and what you desire to say about this post, in my view its in fact
    remarkable for me.

  15. There are various tools and websites that allegation to allow users to view private Instagram profiles, but it’s important to log on these subsequent to caution. Many of these tools can be
    unreliable, may require personal information, or could violate Instagram’s terms of
    service. Additionally, using such tools can compromise your own security or guide to scams.
    The safest and most ethical habit to view a free private instagram viewer app profile is to send a follow request directly to the
    user. Always prioritize privacy and admiration in your online interactions.

  16. Well explained. Discover more at pressure cleaning

  17. BrettTIT表示:

    Viagra generic over the counter: FastPillsForMen.com – Viagra generic over the counter

  18. PatrickfUm表示:

    Alt coin обменник – Альткоин, Альт коин обменник

  19. JacobLoody表示:

    https://www.vizitnews.ru — Посетите официальный сайт vizitnews для подробной информации.

  20. BrettTIT表示:

    cheapest ed treatment: erectile dysfunction online prescription – cheap ed treatment

  21. 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트

  22. jabaとは表示:

    Immigrants from Afghanistan, they first arrived in America within the 1970s.

  23. How Poker And Personal Finance Relate 에볼루션 마틴

  24. Hi there just wanted to give you a brief heads
    up and let you know a few of the pictures aren’t loading correctly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different internet browsers and both show the same results.

發佈留言

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