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,429 Responses

  1. bokep表示:

    Howdy! I understand this is somewhat off-topic but I needed to ask.

    Does running a well-established website such as yours take a large amount of work?
    I am brand new to writing a blog but I do write in my journal
    daily. I’d like to start a blog so I can share my personal experience and views online.
    Please let me know if you have any kind of suggestions or
    tips for brand new aspiring bloggers. Appreciate it!

  2. The Secret Secrets Of Best CSGO Case To Open danger zone case

  3. What Is Best Linkbuilding Software And How To Utilize It
    link builder Software

  4. RandallTaulp表示:

    tamoxifen dose: does tamoxifen cause weight loss – should i take tamoxifen

  5. wayranks.com表示:

    Guide To Locked Myself Out Of My Car: The Intermediate Guide In Locked Myself Out Of My Car locked myself out of my car (wayranks.com)

  6. RandallTaulp表示:

    buy cytotec online fast delivery: Misoprostol 200 mg buy online – buy cytotec pills online cheap

  7. See What Best Rated Robot Vacuum Tricks The Celebs Are Utilizing Best Rated Robot Vacuum

  8. What Semi Truck Lawyer Experts Want You To Be Able To semi truck accident attorneys (http://daywell.kr/)

  9. WilliamRhype表示:

    http://ciprofloxacin.tech/# ciprofloxacin

  10. Davidtes表示:

    lisinopril 1.25 lisinopril generic 20 mg lisinopril 10mg tablet

  11. What Is The Reason Adding A Key Word To Your Life Will Make All The An Impact auto key replacement locksmith

  12. Adhd Symptoms In Adult Women Explained In Less Than 140 Characters Symptoms For Adhd In Females

  13. Great post. I was checking continuously this blog and I am impressed!
    Extremely helpful information specially the last part :
    ) I care for such info much. I was looking for this certain info for a long time.
    Thank you and best of luck.

  14. vo_ntorg_tvot表示:

    Воєнторг
    6. Тактические рюкзаки и сумки для военных
    штани тактичні літні штани тактичні літні .

  15. Davidtes表示:

    buy propecia without rx buy propecia without a prescription buying cheap propecia without prescription

  16. Five Killer Quora Answers On Double Glazing Repairs Leeds
    double glazing repair

  17. Ronaldpsymn表示:

    https://nolvadex.life/# tamoxifen chemo

  18. Margart表示:

    A Peek At Why Are CSGO Skins Going Up In Price’s Secrets Of Why Are CSGO Skins Going Up In Price cs2 cases – Margart

  19. Micah表示:

    There’s A Good And Bad About Fridge Freezer American american fridge freezer sale (Micah)

  20. Ronaldpsymn表示:

    https://nolvadex.life/# aromatase inhibitors tamoxifen

  21. cheaper表示:

    10 Bag Prada Techniques All Experts Recommend cheaper

  22. WilliamRhype表示:

    http://cytotec.club/# buy cytotec in usa

  23. Find Out More表示:

    Hey There. I found your blog using msn. This is an extremely well
    written article. I’ll make sure to bookmark
    it and come back to read more of your useful information.
    Thanks for the post. I’ll certainly return.

  24. Davidtes表示:

    cost for 2 mg lisinopril prinivil 5 mg tablets buy lisinopril mexico

  25. An Adventure Back In Time: What People Discussed
    About Malpractice Compensation 20 Years Ago malpractice Lawyers

  26. The History Of Locksmith For Auto In 10 Milestones auto keys locksmith

  27. Верификация документов необходима
    для вывода со счета сумм более долларов.

  28. 14 Cartoons About Machine Espresso To Brighten Your
    Day espresso machines

發佈留言

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