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,366,788 Responses

  1. Luella Clark表示:

    It’s amazing how much an effective SEO strategy from a good marketing strategies can do for law

  2. Having read this I thought it was rather informative.
    I appreciate you finding the time and energy to put this short article together.
    I once again find myself personally spending way too much time both reading and commenting.
    But so what, it was still worthwhile!

  3. Alta Chavez表示:

    I loved this blog’s focus on ergonomic practices while working, especially since many welders face physical strain over time—great advice inside! For further reading mobile welding

  4. Nikefut表示:

    tor market url dark web market links tor market url

  5. I don’t even understand how I ended up here, but I thought
    this publish was once great. I don’t understand who you might be however certainly you’re going to
    a well-known blogger should you aren’t already. Cheers!

  6. Alan Nguyen表示:

    For those who love baking nang canisters

  7. SamuelBix表示:

    prednisone buy: prednisone canada prescription – prednisone 2 mg

  8. website表示:

    Every operator we cover on this web page is among the many
    12 Michigan sportsbooks which are regulated by the Michigan Gaming Control Board.

    my web page … website

  9. The art scene in Santa Rosa is vibrant santa rosa marketing agency

  10. Nikefut表示:

    bitcoin dark web dark web search engine bitcoin dark web

  11. ThelmaFange表示:

    On our website you will find a unique assortment valuable coins from the whole planet. We offer both modern and historical specimens that will become real finds for collectors.
    For more information, follow the link монеты россии каталог
    Our team of experts carefully selects each coin, ensuring high quality.

  12. Hello there, just became aware of your blog through Google,
    and found that it is really informative. I’m going
    to watch out for brussels. I will appreciate if you continue this in future.
    Lots of people will be benefited from your writing.
    Cheers!

    Stop by my web site; ทางเข้า fun88

  13. RobertJar表示:

    prednisone 60 mg price: Predni Best – buy prednisone no prescription

  14. KennethPah表示:

    where can i buy generic clomid can i purchase generic clomid pill can i get clomid without rx

  15. Energy Sprix表示:

    Thank you for the auspicious writeup. It in fact was a amusement account it.
    Look advanced to more added agreeable from you!
    By the way, how can we communicate?

  16. Gary Daniel表示:

    Just finished my clean-up project using an eco-friendly dumpster from orlando dumpster rental —very

  17. SamuelBix表示:

    buy cipro online: buy cipro online without prescription – ciprofloxacin

  18. KennethPah表示:

    amoxicillin 500mg price Amox Star amoxicillin script

  19. Don’t Buy Into These “Trends” Concerning 3 Wheel Rollator Walker 3 wheeled rollator Walker

  20. Medicines information leaflet. Long-Term Effects.
    can you buy olmesartan pills
    All about medicine. Get information here.

  21. WOW just what I was searching for. Came here by searching for ทางเข้า live casino house

    Check out my blog; ทางเข้า live casino house

  22. The distinction in between these and specialist teeth lightening is the chemical focus.

  23. DanielAbice表示:

    http://prednibest.com/# prednisone in canada

  24. RobertJar表示:

    prednisone 5 mg tablet without a prescription: Predni Best – where to buy prednisone in australia

  25. KennethPah表示:

    amoxicillin 500mg capsule cost amoxicillin cost australia where can you buy amoxicillin over the counter

  26. DanielAbice表示:

    http://prednibest.com/# prednisone 5 tablets

  27. Ethan Mendoza表示:

    I was curious about Nang Gun and this was very helpful! Found extra info at Nang Melbourne

  28. Ora Vega表示:

    This guide on fence construction is super helpful! I’ll definitely refer back to it when I start my project Sports field fencing contractors

發佈留言

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