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,327 Responses

  1. Thanks for sharing your thoughts about Ionic silver.

    Regards

  2. Thanks for every other informative site. Where else could
    I get that kind of info written in such a perfect method?
    I’ve a project that I’m simply now working on, and I’ve been at the glance out for such info.

  3. промокод на подключение робокассаа промокод на подключение робокассаа .

  4. Тут можно преобрести сейф оружейный купить в москве сейф для оружия купить в москве

  5. StevenNub表示:

    amoxicillin 500mg price in canada: amoxil – amoxicillin online no prescription

  6. Dereketerm表示:

    buy prednisone nz: cheap prednisone – apo prednisone

  7. StevenNub表示:

    buy priligy: priligy – Priligy tablets

  8. Laskar303表示:

    Hi, all the time i սsed too check webpage posts һere in the earlʏ hours in the break of ɗay, as і love to
    gain knowledge ⲟf moгe and morе.

    Feel free tο surf tο my webpage: Laskar303

  9. StevenNub表示:

    Priligy tablets: dapoxetine online – Priligy tablets

  10. StevenNub表示:

    over the counter prednisone cheap: order Prednisone – prednisone 5mg capsules

  11. Nikefut表示:

    darknet markets black internet dark web websites

  12. wrapfolie helm

    Nice blog here! Also your web site loads up very fast!
    What host are you using? Can I get your affiliate link to your host?
    I wish my web site loaded up as quickly as yours lol

  13. slot demo表示:

    I do agree with all the ideas you have introduced on your post.
    They’re really convincing and will certainly work. Nonetheless, the posts are
    very short for beginners. May just you please extend them a bit from next time?

    Thanks for the post.

  14. Bitcoin Paris表示:

    In fact when someone doesn’t understand then its
    up to other people that they will help, so here it
    occurs.

  15. JacobLoody表示:

    кухни в москве – Подбор кухонь с установкой в Москве и области.

  16. StevenNub表示:

    cost generic clomid now: clomid rex pharm – order clomid without rx

  17. Medicine information leaflet. What side effects?
    what is meloxicam pills used for
    All information about medication. Get now.

  18. pepek表示:

    Hey! 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 useful information to work on. You have done
    a marvellous job!

  19. Узнай все о степени варикоцеле варикоцеле слева у мужчин

發佈留言

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