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,130,943 Responses

  1. Hello! I know this is kinda off topic but I was wondering which blog
    platform are you using for this site? I’m getting sick and tired of WordPress because I’ve
    had problems with hackers and I’m looking at options
    for another platform. I would be great if you could point me in the direction of
    a good platform.

  2. Drug prescribing information. Short-Term Effects.
    get generic zithromax without rx
    All information about meds. Read information here.

  3. 15 Retro Fridge Freezer Black Benefits Everybody Should Be Able To white Side by side fridge Freezer

  4. ngentot表示:

    I must thank you for the efforts you have put in penning
    this website. I really hope to check out the same high-grade blog posts by you later on as well.
    In fact, your creative writing abilities has
    motivated me to get my very own website now 😉

    Here is my blog – ngentot

  5. Easydrorbix表示:

    pharmacy on line: fluconazole pharmacy – cheapest order pharmacy viagra

  6. Hi this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding know-how so I wanted to get
    advice from someone with experience. Any help would be
    greatly appreciated!

  7. I am sure this paragraph has touched all the internet users, its really really good post on building up new weblog.

  8. Если вы искали где отремонтировать сломаную технику, обратите внимание – ремонт техники в челябинске

  9. 3mmc canada表示:

    It’s genuinely very difficult in this busy life to listen news on Television, thus I
    just use world wide web for that reason, and
    get the most up-to-date information.

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

  11. Shelly表示:

    Are You Getting The Most Out You Reprogramming Car
    Key? reprogramme car key (Shelly)

  12. There’s definately a lot to find out about this topic.
    I like all the points you have made.

  13. Если вы искали где отремонтировать сломаную технику, обратите внимание – ремонт бытовой техники в челябинске

  14. Thanks for your personal marvelous posting! I truly enjoyed reading it,
    you happen to be a great author.I will remember to bookmark your blog and will come back in the foreseeable future.
    I want to encourage you to definitely continue your great job, have a nice
    morning!

  15. TOKEKWIN88表示:

    Wonderful article! We are linking to this great content
    on our site. Keep up the great writing.

  16. KijangSlot表示:

    It’s hard to come by knowledgeable people on this subject, however,
    you seem like you know what you’re talking about!
    Thanks

  17. Best NGO表示:

    Asking questions are in fact pleasant thing if
    you are not understanding anything entirely, however this
    post presents nice understanding yet.

  18. Если вы искали где отремонтировать сломаную технику, обратите внимание – профи ремонт

  19. car表示:

    10 Things That Your Family Teach You About Program Car Key car

  20. Easydrorbix表示:

    online pharmacy cellcept: propecia at boots pharmacy – online pharmacy percocet no prescription

  21. Why To Consider An Unsecured Business Loan 다바오 지진

  22. Easydrorbix表示:

    winn-dixie pharmacy: hong kong pharmacy ambien – cialis pharmacy australia

  23. Drstobeese表示:

    pharmacy store window: reddit best online pharmacy – allergy

  24. leather sofa表示:

    9 . What Your Parents Taught You About L Shape Leather Sofa leather sofa

  25. dream99 login表示:

    It’s amazing designed for me to have a website, which is valuable for my
    know-how. thanks admin

  26. BOMSLOT表示:

    I was excited to uncover this web site. I wanted to thank
    you for your time for this fantastic read!!
    I definitely loved every little bit of it and i also have you saved to fav
    to see new information on your web site.

  27. Easydrorbix表示:

    viagra no prescription online pharmacy: escitalopram generic pharmacy – depakote online pharmacy

  28. Spot on with this write-up, I seriously feel this website needs much more attention. I’ll probably be back again to read through more, thanks for
    the info!

發佈留言

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