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,374,077 Responses

  1. Sweet blog! I found it while searching on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Cheers

  2. StevenNub表示:

    prednisone price: buy prednisone – prednisone 10mg for sale

  3. 5 The 5 Reasons Bmw Smart Key Is Actually A Great Thing diamond Button on bmw Key

  4. Meds prescribing information. Generic Name.
    where to buy generic ceftin pill
    Actual information about drug. Read information now.

  5. What’s up, after reading this remarkable post i am too delighted to share my know-how here with
    friends.

  6. Dereketerm表示:

    priligy: dapoxetine online – Priligy tablets

  7. You actually make it seem so easy with your presentation but I find
    this topic to be really something that I think I would never understand.
    It seems too complicated and very broad for me. I’m looking forward for your next post, I’ll try to get the hang of it!

  8. Nikefut表示:

    darknet drug links dark web drug marketplace https://darknetmarketstore.com/ – darknet links

  9. StevenNub表示:

    prednisone 30: prednisoneraypharm – prednisone 10mg tablet price

  10. 9 . What Your Parents Teach You About Twin Pushchairs twin pushchairs

  11. StevenNub表示:

    max pharm: max pharm – buy priligy

  12. BrandenSlomy表示:

    prednisone 20 mg tablets: ray pharm – prednisone 5mg daily

  13. BrandenSlomy表示:

    prednisone coupon: prednisone – prednisone 30 mg tablet

  14. StevenNub表示:

    apo prednisone: ray pharm – buy prednisone 20mg

  15. Dereketerm表示:

    buy priligy max pharm: priligy maxpharm – dapoxetine price

  16. BrandenSlomy表示:

    generic for amoxicillin: buy amoxil online – amoxicillin 500mg capsule buy online

  17. Nikefut表示:

    dark web site darknet drugs https://darknetmarketstore.com/ – dark web search engine

  18. Drugs prescribing information. Brand names.
    is hydroxyzine like promethazine
    Everything news about medicine. Get now.

  19. I know this website presents quality based posts and other stuff, is there any other web site which presents such data in quality?

  20. It’s an remarkable post in support of all the web users;
    they will take advantage from it I am sure.

  21. StevenNub表示:

    order generic clomid tablets: cheap clomid – can i order generic clomid without insurance

  22. respectively regardless of the learning style of your child or his busy
    schedule, you for sure discover a https://www.oursupporthub.co.uk/india/psh-businesses/edward-evans that
    is real for you suitable.

  23. Iyannaobesk表示:

    jack poker отзывы
    jack poker реальные отзывы

  24. StevenNub表示:

    dapoxetine price: priligy maxpharm – priligy maxpharm

  25. BrandenSlomy表示:

    priligy max pharm: dapoxetine online – priligy

  26. Der “RealPlayer Downloader” bindet sich in verschiedene Browser ein und ermöglicht
    es, kostenlos Videos aus dem Netz zu laden.

  27. techprime表示:

    Excellent beat ! I wish to apprentice while you amend your website, how can i
    subscribe for a blog website? The account helped me
    a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear
    idea

  28. BrandenSlomy表示:

    prednisone 60 mg: prednisoneraypharm – prednisone 1 mg tablet

  29. eb5 india表示:

    Every weekend i used to pay a quick visit this website, as i
    wish for enjoyment, since this this web site conations truly fastidious funny information too.

  1. 2024-11-21

    […] play and two huge rooms for cash games, one fօr reds foг high limit оne particular foг cloudlab.tw lower limits. They give Hold`em, Omaha аnd Stud at virtually any limіt you can think […]

發佈留言

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