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,123,485 Responses

  1. 10 Wrong Answers To Common Multi Fuel Stoves For Sale Questions Do You Know The Right Ones?
    small multi fuel Stoves for sale

  2. 4452346表示:

    Why We Do We Love Custom Sectional Sofa (And You Should Too!)
    4452346

  3. A Guide To Cost Of A Replacement Car Key In 2023 car key replacement Cost

  4. What’s Taking place i’m new to this, I stumbled upon this
    I have discovered It positively useful and it has aided
    me out loads. I hope to give a contribution & assist other customers like its aided me.
    Good job.

  5. I like what you guys are up too. This type of
    clever work and coverage! Keep up the excellent works guys I’ve
    you guys to blogroll.

  6. 7 Helpful Tips To Make The Most Out Of Your Kia
    Key Fob Replacement kia locked keys in car

  7. Testing For ADHD Tools To Ease Your Everyday Lifethe Only Testing For ADHD
    Trick That Every Person Should Be Able To Get tested for adhd

  8. I’m really loving the theme/design of your blog. Do you ever run into any internet browser compatibility problems?
    A small number of my blog readers have complained about
    my site not operating correctly in Explorer but looks great in Chrome.
    Do you have any tips to help fix this issue?

  9. What’s The Job Market For Modular Sleeper Sofa Professionals?

    modular sleeper sofa

  10. Hello, I enjoy reading through your article post. I
    like to write a little comment to support you.

  11. Kennethbaw表示:

    Best Live Casinos (online real-time casino games) have brought a new wave of excitement to the Canadian online casino industry https://ca-livecasino.com/

  12. Drstobeese表示:

    lamictal pharmacy assistance: cheapest online pharmacy india – online pharmacy price checker

  13. What’s The Most Creative Thing Happening With Free Standing Electric
    Fireplaces http://Www.1920416.Xyz

  14. Easydrorbix表示:

    online pharmacy lasix: online pharmacy reviews percocet – viagra sale 70 pharmacy online

  15. The 10 Most Scariest Things About Round Sectional Sofa Round Sectional Sofa

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

  17. Hello! I could have sworn I’ve been to this website before but
    after reading through some of the post I realized it’s new to me.
    Nonetheless, I’m definitely glad I found it and I’ll be bookmarking and checking back
    often!

    Feel free to surf to my blog pronerve 6 reviews

  18. A reputable mesothelioma law firm provides free case evaluations to determine eligibility for
    compensation. The top mesothelioma lawyers operate on a contingency
    basis. This arrangement allows for the highest client compensation.

    my blog post :: asbestos attorney

  19. Alvin表示:

    Howdy, I think your site may be having internet browser compatibility problems.
    When I take a look at your site in Safari, it looks fine however when opening in Internet Explorer,
    it has some overlapping issues. I merely wanted to
    provide you with a quick heads up! Other than that, wonderful
    website!

  20. Katherin表示:

    Please let me know if you’re looking for a article writer for your weblog.
    You have some really great posts and I feel I would be a good asset.
    If you ever want to take some of the load off, I’d absolutely love to write some content for your blog in exchange for a link back to mine.
    Please send me an e-mail if interested. Many thanks!

  21. Easydrorbix表示:

    discount pharmacy: true rx pharmacy – which pharmacy has tamiflu

  22. Kala表示:

    Whoa! This blog looks just like my old one! It’s on a completely
    different topic but it has pretty much the same layout and design. Great choice
    of colors!

  23. What’s up i am kavin, its my first occasion to commenting anyplace, when i read
    this piece of writing i thought i could also create comment due to this sensible post.

  24. I’ve been browsing on-line greater than three hours nowadays, but I never discovered any fascinating article like yours.

    It’s beautiful value sufficient for me. Personally, if all web owners and bloggers made excellent
    content as you did, the net will be a lot more useful than ever before.

  25. Easydrorbix表示:

    viagra online pharmacy uk: voltaren emulgel online pharmacy – bradleys pharmacy artane

  26. Great post! We are linking to this great article on our website.
    Keep up the great writing.

  27. Meds prescribing information. Drug Class.
    how can i get generic fosamax without dr prescription
    Some information about medicament. Get information here.

  28. Easydrorbix表示:

    adipex pharmacy card: omeprazole tesco pharmacy – rx hmong pharmacy

發佈留言

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