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,449,712 Responses

  1. Ahaa, its pleasant conversation about this article at this place at this webpage, I
    have read all that, so at this time me also commenting at this place.

  2. CurtisBam表示:

    best mail order pharmacy canada: Canadian Md Pharm – vipps canadian pharmacy

  3. indian porn表示:

    hello!,I love your writing very much! share we keep up a
    correspondence extra about your article on AOL? I need a specialist in this area
    to solve my problem. Maybe that is you! Taking a look forward to see
    you.

  4. Mason Marsh表示:

    I’ve seen what clogged gutters can do to houses around here Gutter Cleaning Amherst

  5. CurtisBam表示:

    mexico pharmacies prescription drugs: medication from mexico pharmacy – mexican drugstore online

  6. Brandonnot表示:

    Mexican Easy Pharm: Mexican Easy Pharm – mexican border pharmacies shipping to usa

  7. Hello! I know this is kind of off topic but I was wondering
    which blog platform are you using for this website?

    I’m getting sick and tired of WordPress because I’ve had problems with hackers
    and I’m looking at alternatives for another platform.
    I would be awesome if you could point me in the direction of a good platform.

  8. CurtisBam表示:

    canada cloud pharmacy: canadian pharmacy prices – canadian pharmacy meds

  9. Great website! It looks extremely professional!
    Keep up the helpful job!
    https://infoskierniewice.pl

  10. CurtisBam表示:

    canadian drugs online: canadian pharmacy ed medications – canadapharmacyonline legit

  11. Much thanks! It is definitely an incredible web page!
    https://gostyn24.pl

  12. This is very insightful. Check out بلفاروپلاستی چیست for more

  13. Carrolleruck表示:

    https://indiancertpharm.com/# Indian Cert Pharm
    my canadian pharmacy

  14. Oh mmy goodness! Amazing article dude! Ⅿany tһanks, Howevеr Ι am experiencing issues ѡith yߋur RSS.
    I don’t know the reason wһy I am unable to sybscribe to іt.

    Is thwre anyboɗy else getting sіmilar RSS problems? Anyone ԝho knows the answer caan ʏⲟu kindly respond?
    Tһanks!!

    my page … đồng hồ nữ đẹp chính hãng

  15. Brandonnot表示:

    mexican pharmaceuticals online: mexico drug stores pharmacies – Mexican Easy Pharm

  16. memek basah表示:

    Thanks in support of sharing such a good idea, paragraph is pleasant,
    thats why i have read it completely

  17. Allie Shaw表示:

    Passive income through rental properties is one of the most appealing aspects of real estate investing sell my house fast austin

  18. The No. 1 Question That Anyone Working In Grey Fabric
    Three Seater Sofa Must Know How To Answer 3 seater grey fabric Sofa

  19. CurtisBam表示:

    Indian Cert Pharm: Indian pharmacy that ships to usa – Online pharmacy

  20. Thank you so much! This is definitely an fantastic webpage!

    https://www.gazetamiedzyszkolna.pl

  21. You’re an extremely valuable internet site; could not make it without ya!

    https://swiecie24.pl/pl/

  22. Brandonnot表示:

    IndianCertPharm: Indian pharmacy that ships to usa – Online medicine

  23. Brandonnot表示:

    Indian Cert Pharm: Indian Cert Pharm – Indian Cert Pharm

  24. 11 Ways To Completely Redesign Your Pragmatic Official Website 프라그마틱 환수율

發佈留言

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