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,477,537 Responses

  1. websell begok表示:

    Hmm it looks like your blog ate my first comment (it
    was super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying
    your blog. I too am an aspiring blog blogger but I’m still new to the whole thing.

    Do you have any suggestions for rookie blog writers?

    I’d genuinely appreciate it.

  2. DarrenUnjuh表示:

    https://cytpremium.com/# buy cytotec

  3. 유흥表示:

    Cocktail Lounge 유흥

  4. DarrenUnjuh表示:

    https://cytpremium.com/# buy cytotec pills online cheap

  5. DarrenUnjuh表示:

    http://lisinopril1st.com/# Lisinopril 1st

  6. SHLPURDC6表示:

    Hi would you mind letting me know which hosting company you’re working with?
    I’ve loaded your blog in 3 completely different
    web browsers and I must say this blog loads a lot quicker
    then most. Can you suggest a good web hosting provider at a honest price?
    Many thanks, I appreciate it!

  7. Hi are using WordPress for your site platform? I’m new to the
    blog world but I’m trying to get started and
    create my own. Do you require any html coding expertise to make your own blog?
    Any help would be greatly appreciated!

  8. With havin so much content do you ever run into any problems
    of plagorism or copyright infringement? My site has a lot of
    exclusive content I’ve either authored myself or outsourced but
    it looks like a lot of it is popping it up all over the web without my
    authorization. Do you know any solutions to help protect against content from being stolen? I’d truly appreciate it.

  9. DarrenUnjuh表示:

    http://lisinopril1st.com/# Lisinopril 1st

  10. 10 Meetups On Electric Stove Fireplace You Should Attend electric fireplace stove

  11. AlbertDuM表示:

    buy cytotec pills: buy cytotec online – buy cytotec pills online cheap

  12. Equinunk表示:

    Howdy! This is my first visit to your blog! We are a
    collection of volunteers and starting a new initiative in a community in the same
    niche. Your blog provided us beneficial information to work
    on. You have done a extraordinary job!

  13. Nikefut表示:

    best darknet markets darknet websites https://mydarknetmarketlinks.com/

  14. BrandenSlomy表示:

    buy priligy max pharm: max pharm – priligy maxpharm

  15. Helen Black表示:

    This guide has me considering an upgrade to my office with some sleek laminate flooring—thanks! Hill Country Flooring & Construction

  16. AlbertDuM表示:

    cytotec buy online usa: cyt premium – buy cytotec

  17. BernardMop表示:

    buy cytotec online buy cytotec online Cytotec 200mcg price

  18. вывод из запоя с выездом краснодар вывод из запоя с выездом краснодар .

  19. BrandenSlomy表示:

    can i order clomid for sale: can i buy clomid without prescription – order clomid pills

  20. Hey There. I found your blog using msn. This is an extremely well written article.
    I’ll make sure to bookmark it and come back
    to read more of your useful information. Thanks for the
    post. I will definitely comeback.

發佈留言

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