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,243,508 Responses

  1. Excellent pieces. Keep posting such kind of information on your blog.
    Im really impressed by it.
    Hey there, You have performed a great job.
    I will certainly digg it and for my part suggest to my friends.

    I’m confident they will be benefited from this website.

  2. AlbertDuM表示:

    stromectol 3 mg dosage: buy Stromectol – ivermectin cream

  3. AlbertDuM表示:

    stromectol generic name: iver fast – order stromectol

  4. Very great post. I just stumbled upon your blog and wanted to say that I have truly enjoyed surfing around your weblog posts.
    In any case I’ll be subscribing on your feed and I’m hoping you write once more soon!

  5. AlbertDuM表示:

    cytotec pills buy online: buy cytotec online – cytotec online

  6. That is a really good tip especially to those fresh to the blogosphere.
    Brief but very precise information… Appreciate your sharing
    this one. A must read article!

  7. AlbertDuM表示:

    zestril brand name: buy Lisinopril online – buy Lisinopril online

  8. BernardMop表示:

    buy stromectol buy Ivermectin online minocycline 100mg acne

  9. Thank you for some other informative site.
    The place else may just I get that type of information written in such an ideal method?
    I have a undertaking that I am just now running on, and I have been at the look out for such info.

  10. mindful表示:

    Hello I am so excited I found your blog page, I really found you
    by accident, while I was searching on Askjeeve for something else, Regardless I am here now and would just like to say thanks for a
    incredible post and a all round interesting blog (I also love the theme/design), I don’t have time to browse it all at the minute but I have saved
    it and also added in your RSS feeds, so when I have time I will be back to read much
    more, Please do keep up the fantastic work.

  11. google表示:

    Wow that was strange. I just wrote an incredibly long comment but after I
    clicked submit my comment didn’t show up. Grrrr…
    well I’m not writing all that over again. Regardless,
    just wanted to say fantastic blog!

  12. BrandenSlomy表示:

    prednisone 50: cheap prednisone – where can i get prednisone

  13. Nikefut表示:

    tor marketplace dark web search engine https://mydarknetmarketlinks.com/

  14. I couldn’t resist commenting. Well written!

  15. DarrenUnjuh表示:

    https://cytpremium.com/# п»їcytotec pills online

  16. ww88-now表示:

    It’s remarkable to visit this web site and reading the views of all friends on the topic of this article,
    while I am also keen of getting familiarity.

  17. BrandenSlomy表示:

    amoxicillin 250 mg price in india: amoxil com pharm – generic amoxicillin

  18. Medicine information leaflet. What side effects can this medication cause?
    where can i get chlorpromazine pill
    Actual information about medication. Read information here.

  19. click now表示:

    For hottest information you have to pay a visit internet and on web I found this site as a finest
    web site for latest updates.

  20. nodeposit表示:

    It’s appropriate time to make some plans for the longer term and it’s time to be happy.

    I have learn this put up and if I may just I wish to recommend you few attention-grabbing issues or tips.

    Perhaps you can write subsequent articles relating to this article.
    I want to learn even more things approximately it!

  21. It’s truly a great and helpful piece of info.
    I’m satisfied that you simply shared this useful info with us.
    Please stay us up to date like this. Thank you for sharing.

  22. Nikefut表示:

    tor market links tor markets links https://mydarknetmarketlinks.com/

  23. It’s remarkable in favor of me to have a site, which is good in support of my know-how.
    thanks admin

  24. George Valdez表示:

    If you remain in the Phoenix area and wish to enhance your online presence, you ought to certainly look into Phoenix SEO services Digitaleer Phoenix SEO & Web Design

發佈留言

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