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,421,762 Responses

  1. Prisma Case表示:

    Do You Think You’re Suited For CSGO Most Profitable Cases?

    Try This Quiz Prisma Case

  2. Davidtes表示:

    tamoxifen citrate tamoxifen and grapefruit tamoxifen rash pictures

  3. 10 Reasons You’ll Need To Learn About Car Key Cover Fiat 500 Fiat panda Key programming

  4. RandallTaulp表示:

    nolvadex price: femara vs tamoxifen – tamoxifen hot flashes

  5. Ronaldpsymn表示:

    http://finasteride.store/# generic propecia without prescription

  6. A Positive Rant Concerning Upvc Window Repairs Upvc Window Repairs Near Me

  7. Davidtes表示:

    order lisinopril from mexico zestril lisinopril 15 mg tablets

  8. I am extremely inspired along with your writing skills as neatly as with the format for your
    blog. Is that this a paid topic or did you modify it yourself?
    Either way stay up the excellent high quality writing, it’s rare to peer a nice weblog like
    this one these days..

    Here is my web page :: Gold filled jewelry gifts

  9. Colette表示:

    Misted Double Glazing Repairs Tools To Ease Your Daily Life Misted Double
    Glazing Repairs Trick That Should Be Used By Everyone Learn double glazing repairs (Colette)

  10. Davidtes表示:

    lisinopril 25 mg tablet lisinopril 500 mg drug prices lisinopril

  11. Car key Volvo表示:

    Everything You Need To Know About Volvo Digger Key
    Dos And Don’ts Car key Volvo

  12. Davidtes表示:

    cipro 500mg best prices cipro online no prescription in the usa buy cipro online without prescription

  13. What’s The Job Market For Car Key Repair Shop Near Me Professionals Like?

    Key Repair Shop Near Me

  14. The Best Online Shopping Uk Electronics Tricks To Rewrite Your Life Comfortable Breastfeeding Pads

  15. How Window Repair Near Has Become The Most Sought-After Trend Of 2023
    window repair Near Me

  16. Injury Legal Explained In Less Than 140 Characters injury Lawsuit

  17. How To Create Successful Car Lock Repair Near Me Instructions
    For Homeschoolers From Home car central locking repair near Me

  18. RandallTaulp表示:

    propecia rx: cost of cheap propecia online – get generic propecia pills

  19. This Is How Bunk Beds Double Bed Will Look Like In 10 Years double bed Bunk – https://www.dermandar.com/

  20. RandallTaulp表示:

    cheapest price for lisinopril: lisinopril 25 mg cost – lisinopril cost uk

  21. RandallTaulp表示:

    tamoxifen breast cancer prevention: tamoxifen breast cancer prevention – tamoxifen citrate pct

  22. WilliamRhype表示:

    https://lisinopril.network/# buy lisinopril 20 mg online united states

  23. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 10 mg on line prescription

  24. Leonardo表示:

    Do Not Believe In These “Trends” Concerning Robot Vacuum And Mops robot vacuum
    and mop self empty – Leonardo

  25. WilliamRhype表示:

    http://finasteride.store/# buying propecia tablets

  26. window repair表示:

    The 10 Most Scariest Things About Pvc Window Repairs window repair

  27. Need Inspiration? Check Out Lock Smith For Cars Car Lock Smith

  28. WilliamRhype表示:

    https://finasteride.store/# cheap propecia without insurance

  29. What’s up, I want to subscribe for this blog to take
    newest updates, thus where can i do it please help out.

發佈留言

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