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,245,698 Responses

  1. coupon codes表示:

    Hello great blog! Does running a blog similar to this take a
    great deal of work? I’ve no understanding of coding however I was hoping to start my own blog soon.
    Anyhow, should you have any recommendations or techniques for new blog owners
    please share. I know this is off subject but I just needed to ask.
    Kudos!

  2. Bet365 Bingo表示:

    Appreciate the recommendation. Let me try it out.

    Feel free to visit my web blog – Bet365 Bingo

  3. Mollie Lane表示:

    I’m all about no deposit grants! They give me a likelihood to win with no risking my possess payment https://files.fm/u/xmrxtxqpj7

  4. BernardMop表示:

    ivermectin ebay IverFast ivermectin 0.5

  5. At this moment I am ready to do my breakfast, after having my breakfast coming over again to read more news.

  6. DarrenUnjuh表示:

    http://cytpremium.com/# buy cytotec pills

  7. BrandenSlomy表示:

    prednisone 1 tablet: prednisone – prednisone buy

  8. DarrenUnjuh表示:

    http://iverfast.com/# minocycline 100 mg without prescription

  9. Brett Lee表示:

    Beyond grateful recognizing efforts given outside realms normal expectations continually pushing boundaries seeking foster change amongst peers encourages growth seen everywhere today reflecting back positively onto futures envisioned collectively Medicare Annual Enrollment

  10. AlbertDuM表示:

    price of stromectol: Ivermectin iver fast – ivermectin otc

  11. BernardMop表示:

    generic plavix Plavix Clo cheap plavix antiplatelet drug

  12. 오피表示:

    Full Service Spa 오피

  13. Узнай все о увеличение полового члена хирургическим путем хирургическое увеличение члена

  14. Hello to every body, it’s my first visit of this web site; this
    website consists of remarkable and truly fine information in favor
    of visitors.

  15. Emily Burgess表示:

    If you’re in the Phoenix location and wish to boost your online existence, you ought to absolutely check out Phoenix SEO services Digitaleer Phoenix Web Design

  16. BernardMop表示:

    Clopidogrel 75 MG price cheapest plavix Cost of Plavix without insurance

  17. DarrenUnjuh表示:

    https://iverfast.com/# minocycline 100mg for acne

  18. Very quickly this web page will be famous amid all blogging and site-building people,
    due to it’s fastidious content

    my webpage :: luxury assisted living

  19. you’re truly a excellent webmaster. The web site loading velocity
    is incredible. It seems that you are doing any distinctive
    trick. Furthermore, The contents are masterpiece. you have done a
    magnificent task in this topic!

  20. Yes, you can download videos on your mobile phone or tablet.

  21. Chester Lyons表示:

    I found your discussion around privacy policies attorney ppc management companies

  22. Very nice post. I just stumbled upon your weblog and wished to say that I have really enjoyed browsing your blog posts.

    In any case I’ll be subscribing to your feed and I hope you write again very soon!

  23. Узнай все о операция по удлинению полового члена увеличить член цена

發佈留言

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