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,234,093 Responses

  1. Michaelcew表示:

    https://indiaph24.store/# online shopping pharmacy india

  2. I really like it when folks come together and share opinions.
    Great blog, continue the good work!

  3. This Is The History Of GSASER get gsa ser list

  4. The Top 5 Reasons People Win Within The Medical
    Malpractice Litigation Industry Medical malpractice lawsuit

  5. 5 Clarifications On Mesothelioma Legal Question asbestos claim

  6. Slot Pulsa表示:

    This post ѡill assist tһe internet ᥙsers for setting up new website οr evn a weblog ftom start too end.

    Feel free tօ surf to mʏ web page: Slot Pulsa

  7. 10 Failing Answers To Common Small Espresso
    Machine Questions Do You Know The Right Answers?
    Espresso home machine

  8. Ten Washing Dryer Combos That Really Make Your Life Better washer dryer Machine

  9. There is certainly a great deal to know about
    this issue. I really like all the points you’ve made.

  10. Marcelabelp表示:

    https://indiaph24.store/# Online medicine order

  11. Ten Upvc Window Repairs That Really Change Your Life Window repairs

  12. fuck表示:

    It’ѕ ver effortless to find out any matter on net aas compared to books, as I
    foսnd this post at this websitе.

  13. 7 Secrets About Ford Key Repair That No One Will Tell You Ford Focus Key Replacement

  14. Take a look at my web site facebook hookups

  15. Hi Dear, are you genuinely visiting this site daily, if so afterward you will definitely take nice experience.

  16. Rickysem表示:

    buy prescription drugs from india buy medicines from India Online medicine order

  17. Marcelabelp表示:

    http://indiaph24.store/# indian pharmacy

  18. Buzzwords De-Buzzed: 10 Other Ways To Say Washer & Dryer Combo washer & dryer combo

  19. The 10 Scariest Things About Classic Casino Slots
    Classic Casino slots

  20. 5 Laws That Will Help In The Repairs To Double Glazed Windows Industry
    window Repairs

  21. What To Focus On When Enhancing Mesothelioma Lawsuit asbestos lawsuit

  22. Five Killer Quora Answers To Best Woodburners Best woodburners (http://www.mapleprimes.com)

  23. How To Design And Create Successful Wall Mounted Fireplace Strategies From Home electric fireplaces on the
    wall; http://isaevclub.ru/user/stephot3/,

  24. Marcelabelp表示:

    http://canadaph24.pro/# canada drugs online review

  25. Marcelabelp表示:

    https://mexicoph24.life/# medication from mexico pharmacy

  26. Tһis design is steller! You obviously know how to keep a readеr amused.
    Betrween your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!)
    Great job. I really enjoyed what you had tto say, and more than that, how you presented іt.
    Too cool! http://www.scullycap.com/comment/html/?246399.html

  27. Is Top 10 Online Shopping Sites In Uk For Clothes As Important As Everyone Says?
    Fast Roofing Coil Nailer

  28. 5 Killer Quora Answers To Walking Machine Under Desk Walking Machine Under Desk

發佈留言

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