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,432,859 Responses

  1. web scam表示:

    If some one needs expert view about blogging after that i recommend him/her to visit
    this web site, Keep up the nice job.

  2. Incredible a lot of terrific tips!

    Here is my website: https://gaynewengland.net

  3. Near表示:

    %%

    Here is my blog post … Near

  4. My spouse and I stumbled over here different website and thought I should check
    things out. I like what I see so now i am following you.
    Look forward to looking at your web page repeatedly.

  5. Many groups accept as true that they have no option to get a personal loan if they have bad credit, theres high-quality information for you.
    Tip: If you are a teenager, it is important to understand that once your name is on a credit card or debit card, you are beginning to build up a credit history that will follow you for
    decades. You can find many loans options with traditional lenders
    that are available at your place.

  6. Appreciate this post. Let me try it out.

  7. Lonny表示:

    %%

    Take a look at my webpage best case csgo; Lonny,

  8. Pretty! This was an incredibly wonderful post. Thank you for
    providing this information.

  9. Enriqueta表示:

    I was looking at some of your blog posts on this internet site
    and I conceive this website is very informative! Retain posting.

  10. buttmuncher表示:

    Heʏ! Someone in myy Myspɑce group shared thyis website with us so I came to take a lоok.
    І’m definitely lving the informatіon. I’m boⲟk-maгking aand will be tweeting this to my followers!
    Wonderful blog and wonderful design.

  11. Leewhan.Com表示:

    %%

    Feel free to visit my blog post … Car Seat Key (Leewhan.Com)

  12. Maisie Bogner表示:

    You actually explained it really well!

  13. Oh my goodness! Impressive article dude! Мany thаnks,
    Hoᴡeveг Ι am encountering troubles ѡith уouг RSS.
    I don’t know why I cannot subscribe tօ it. Іs theгe anyboԁy else having identical RSS ρroblems?
    AnyƄody who knowѕ thе answer can yοu kindly respond?
    Thanx!!

    Here is mʏ homepaցe; beli followers ig permanen terpercaya

  14. Hi there! I just wanted to ask if you ever have any issues
    with hackers? My last blog (wordpress) was hacked and I ended up losing months of hard work due to
    no back up. Do you have any solutions to prevent
    hackers?

  15. I really like it when people get together and share thoughts.
    Great website, keep it up!

  16. cheap flights表示:

    It’s an awesome article in favor of all the web viewers; they will obtain benefit from it I am sure.

  17. analyze quote表示:

    %%

    Look at my web page: analyze quote

  18. Incredible quest there. What happened after? Thanks!

  19. Thank you for some other great article. The place else
    may just anyone get that kind of info in such an ideal
    manner of writing? I have a presentation subsequent week, and I’m at
    the search for such information.

  20. Can I simply just say what a comfort to discover somebody who genuinely knows what
    they’re talking about on the net. You certainly understand how to bring a problem to light
    and make it important. More and more people ought to look at this and understand this side of your story.
    It’s surprising you aren’t more popular because you most certainly
    have the gift.

  21. Lionel表示:

    asbestos mesothelioma compensation; Lionel,

  22. of course like your web site however you need to test the spelling on several of your posts.
    Many of them are rife with spelling problems and I find it very troublesome to inform the
    truth on the other hand I’ll definitely come again again.

  23. Your Family Will Be Thankful For Getting This Railroad class action lawsuit Lawsuit Interstitial Lung Disease

  24. Heya i am for the first time here. I came across this board and I find It really useful & it helped me out much.

    I hope to give something back and aid others like you aided me.

  25. airav.cc表示:

    %%

    my site – work home From jobs (airav.cc)

  26. Thanks , I’ve just been searching for info approximately
    this subject for a long time and yours is the
    greatest I have found out so far. However, what concerning the conclusion? Are you sure about the supply?

發佈留言

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