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,363,274 Responses

  1. I’m not positive where you are getting your information, however great topic.
    I must spend a while studying more or figuring out more.
    Thank you for magnificent info I used to be in search of
    this information for my mission.

  2. %%

    Feel free to visit my homepage work For avon online

  3. We are a gaggle of volunteers and starting a new scheme in our
    community. Your web site provided us with useful info to work on.
    You have done a formidable task and our entire neighborhood shall
    be thankful to you.

  4. I believe everything posted was very logical. But, what about
    this? suppose you added a little content? I ain’t suggesting your information isn’t solid.,
    however suppose you added a title that grabbed a person’s attention? I mean JS
    依照元素出現數量排序 – 馬老師 雲端研究室 is kinda boring.
    You could peek at Yahoo’s home page and see how they create post headlines to grab viewers to
    open the links. You might add a video or a related pic or two to grab readers interested about
    what you’ve got to say. Just my opinion, it might bring your posts a little livelier.[url=https://monochrome-flamingo-fp09md.mystrikingly.com/blog/3e51c596336]รีแมพคืออะไร[/url]

    รีแมพคืออะไร

  5. service表示:

    %%

    Stop by my homepage … service

  6. teach表示:

    Hi, i read your blog occasionally and i own a similar one and i was just curious if you get a
    lot of spam feedback? If so how do you reduce it, any plugin or anything
    you can advise? I get so much lately it’s driving me crazy so any help is very much
    appreciated.

  7. After checking out a handful of the articles on your
    site, I seriously like your technique of writing a blog.
    I saved as a favorite it to my bookmark webpage list and will be checking back soon. Please visit my web
    site as well and tell me what you think.

  8. Sadie表示:

    %%

    My blog post … cbd topical online store (Sadie)

  9. Will表示:

    %%

    My page 24 hour emergency electrician near me (Will)

  10. Are you tired of encountering issues with your Cash App account?
    It’s time to buy a verified Cash App account! With a verified
    account, you can enjoy a seamless experience without any restrictions.
    Say goodbye to payment obstacles and discover a hassle-free way
    of managing your finances. Don’t wait, purchase a verified
    Cash App account today!

  11. Fine way of explaining, and pleasant post to get data about my presentation subject, which i
    am going to deliver in academy.

  12. Reynaldo表示:

    designer yellow handbags (Reynaldo)

  13. We absolutely love your blog and find many of your post’s to be what precisely I’m
    looking for. can you offer guest writers to write content for you
    personally? I wouldn’t mind writing a post or elaborating
    on most of the subjects you write with regards to here. Again, awesome web log!

  14. I’d like to find out more? I’d like to find out some additional information.

  15. Hugh表示:

    %%

    My website – work from home jobs online – Hugh,

  16. Looking to buy a verified Cash App account? Look no further!
    A verified Cash App account ensures seamless transactions and increased security.

    Don’t risk your money on unverified accounts. Invest in a verified Cash App account today and enjoy a
    hassle-free experience.

  17. Slimex365.Com表示:

    %%

    My page; How To Get Diagnosed With Adhd Uk (Slimex365.Com)

  18. Hi there! I know this is somewhat off topic but I was wondering if you knew where I could get a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having problems finding one?

    Thanks a lot!

  19. Keri表示:

    %%

    Review my homepage: window doctor york (Keri)

  20. 420.Bio表示:

    %%

    my website … Ford Ka Key Replacement (420.Bio)

  21. Looking to buy a verified Cash App account? Look no further!
    Purchasing a verified account ensures added security and trust, allowing you
    to transact with peace of mind. With a verified
    Cash App account, you can easily send and receive payments, manage your finances, and access additional
    features. Don’t delay, buy your verified
    Cash App account today and enjoy hassle-free transactions!

發佈留言

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