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,231,885 Responses

  1. Davidtes表示:

    buy cytotec pills buy cytotec in usa purchase cytotec

  2. 20 Things Only The Most Devoted Mesothelioma Settlement Fans Should Know Asbestos Legal

  3. 15 Things You Didn’t Know About Ford Key Fob Replacement Ford Focus Replacement Key

  4. Nine Things That Your Parent Taught You About Replace Mazda Key
    Replace Mazda Key

  5. Why You’ll Want To Read More About Accident Litigation Accident law Firm

  6. Davidtes表示:

    10mg generic 10mg lisinopril lisinopril pill 20mg buy prinivil

  7. How Volkswagen Car Key Replacement Is A Secret Life Secret Life Of Volkswagen Car Key Replacement Volkswagen Beetle Key replacement

  8. 11 Creative Methods To Write About Designer Bags Cheap designer Bags

  9. WilliamRhype表示:

    http://ciprofloxacin.tech/# purchase cipro

  10. Live Sdy表示:

    Write more, thats all I have to say. Literally, it seems as
    though you relied on the video to make your point.
    You definitely know what youre talking about, why waste
    your intelligence on just posting videos to your
    blog when you could be giving us something
    informative to read?

  11. Davidtes表示:

    cytotec abortion pill buy cytotec pills buy cytotec in usa

  12. Davidtes表示:

    tamoxifen for gynecomastia reviews liquid tamoxifen tamoxifenworld

  13. WilliamRhype表示:

    http://finasteride.store/# cheap propecia without rx

  14. Manuel表示:

    14 Businesses Are Doing A Fantastic Job At CSGO Weapon Case huntsman weapon case (Manuel)

  15. How To Build A Successful Mazda Dealership Key Replacement Even If You’re Not Business-Savvy Mazda Key Fobs

  16. After looking at a number of the blog posts on your blog, I honestly like your way
    of writing a blog. I saved as a favorite it to my bookmark webpage list and will be
    checking back in the near future. Take a look at my web site too and let me
    know your opinion.

    Also visit my web site :: cancer research

  17. slot838表示:

    Spot on with this write-up, I really believe that this amazing site needs far more attention. I’ll probably
    be back again to read more, thanks for the info!

  18. 12 Companies That Are Leading The Way In Managing Adhd Without Medication Prescribe Medications

  19. Hi, i read your blog from time to time and i own a
    similar one and i was just curious if you get a lot of spam comments?

    If so how do you prevent it, any plugin or anything
    you can suggest? I get so much lately it’s driving
    me insane so any support is very much appreciated.

  20. What’s up everyone, it’s my first go to see at this web site, and post is actually fruitful
    in support of me, keep up posting these types of posts.

    Look into my homepage :: 해외선물 대여계좌

  21. RandallTaulp表示:

    cipro ciprofloxacin: cipro generic – ciprofloxacin 500 mg tablet price

  22. Davidtes表示:

    order cytotec online buy cytotec over the counter cytotec pills buy online

  23. Attractive section of content. I just stumbled upon your website and in accession capital to assert that I acquire actually
    enjoyed account your blog posts. Anyway I will be subscribing for your feeds and even I success you access consistently
    rapidly. https://www.thegxpcouncil.com/forums/users/rodolfohennings/

  24. The Reason You Shouldn’t Think About Enhancing Your Auto Locksmiths
    mobile auto locksmith (https://Te.legra.ph/)

  25. mt5 terminal表示:

    Nice meet up with you, i’m Robena nonetheless don’t like
    when people use my full address. Hiring is
    what I do for cash. She is really fond of to solve puzzles but she’s been taking on new
    things lately. Her family lives in Maryland and might never travel.

  26. Ai Rewriter表示:

    Here’s A Few Facts About Ai Content Rewriter Ai Rewriter

  27. What’s Happening i am new to this, I stumbled
    upon this I have found It positively helpful
    and it has aided me out loads. I’m hoping to contribute & assist other customers like its helped me.
    Great job.

  28. 10 Things Everyone Hates About Online Shopping Sites
    London Online Shopping Sites London Blue Kitchen Dishes Set

發佈留言

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