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,229,186 Responses

  1. 5 Asbestos Mesothelioma Attorney Lessons From Professionals Mesothelioma Lawsuits

  2. This is really interesting, You are a very skilled blogger.
    I have joined your rss feed and look forward to seeking more of your magnificent post.

    Also, I’ve shared your site in my social networks!

  3. 10 Reasons That People Are Hateful To Folding Treadmills UK Folding Treadmills UK
    treadmill that folds up

  4. See What Lightest 3 Wheel Stroller Tricks The Celebs Are Utilizing lightest 3 wheel stroller

  5. HassanbuifS表示:

    https://casinositeleri.win/# guvenilir casino siteleri
    deneme bonusu veren siteler yeni

  6. Wonderful post however , I was wanting to know if you could write a
    litte more on this topic? I’d be very thankful if
    you could elaborate a little bit more. Appreciate it!

    https://ranitidinex.com/

  7. Laos Pools表示:

    This site certainly has all the info I wanted concerning this subject and didn’t know who to
    ask.

    https://w8.livedrawlaos.life/

  8. Derekfar表示:

    deneme bonusu veren siteler mycbet.com: deneme bonusu veren siteler yerliarama.org – deneme bonusu veren siteler 2024

  9. ClydePab表示:

    matadorbet bid: matadorbet bid – matadorbet

  10. Тут можно преобрести огнестойкие сейфы купить сейф огнестойкий

  11. What’s The Current Job Market For Best Self-Propelled Wheelchair Professionals?
    best Self-Propelled wheelchair

  12. LarryCoell表示:

    slot tr online en kazancl? slot oyunlar? slot oyunlar?

  13. LarryCoell表示:

    matadorbet matadorbet giris matadorbet

  14. 10 Meetups On Upvc Window Replacement Hinges You Should
    Attend Window Hinge repair Cost

  15. Diplomi_ovkn表示:

    купить диплом в куйбышеве купить диплом в куйбышеве .

  16. Hello friends, pleasant article and good arguments commented at this place, I am genuinely enjoying by
    these.

    https://w2.livetogelhk.top/

  17. ClydePab表示:

    Canl? Casino Siteleri: Deneme Bonusu Veren Siteler – Deneme Bonusu Veren Siteler

  18. Five Killer Quora Answers To Mens Masturbators Mens masturbators

  19. You have made some really good points there. I checked on the web for more info about the issue and found most people will go along with your views on this site.

  20. 10 Essentials Regarding Private Diagnosis ADHD
    You Didn’t Learn At School adhd assessment for Adults private

  21. Spot on with this write-up, I actually think this website needs far
    more attention. I’ll probably be returning to see more, thanks for the info!

    https://w7.datahk.world/

  22. Hello There. I found your blog using msn. This is a
    very well written article. I will make sure to bookmark it and return to read more of your useful information. Thanks for the post.
    I will certainly return.

  23. duct split表示:

    mohajer-co.com
    Good day I am so delighted I found your blog, I really found
    you by mistake, while I was researching on Askjeeve for something else, Nonetheless I am here now and would just like to say many thanks for a tremendous post and a all round interesting blog (I also love the
    theme/design), I don’t have time to browse it
    all at the moment but I have bookmarked it and
    also added in your RSS feeds, so when I have time I will be
    back to read a great deal more, Please do keep up the great work.

發佈留言

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