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,265,864 Responses

  1. Jamesdiure表示:

    казино вавада: вавада казино зеркало – вавада онлайн казино

  2. slot gacor表示:

    Hey! This is my 1st comment here so I just wanted to give a quick shout out and tell you I
    genuinely enjoy reading your articles. Can you recommend any
    other blogs/websites/forums that deal with the same subjects?

    Thanks!

  3. Jamesdiure表示:

    пин ап казино: пин ап кз – пин ап кз

  4. HenrydeT表示:

    pinup kazi pinup пин ап казино

  5. бухгалтерия проф цена 1s-buhgalteriya-kupit.ru .

  6. HenrydeT表示:

    казино вавада вавада онлайн казино vavada kazi

  7. Pol88表示:

    This is very attention-grabbing, You are an excessively skilled blogger.
    I’ve joined your rss feed and stay up for in search of more of your excellent post.
    Also, I have shared your web site in my social networks

  8. DarioTob表示:

    пин ап казино официальный сайт: pinup – пин ап зеркало

  9. Hi, i think that i saw you visited my website thus i came to “return the favor”.I am trying to find things to enhance my site!I suppose its ok to use a few of your ideas!!

  10. Wow that was unusual. I just wrote an really long comment but after I
    clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over
    again. Anyhow, just wanted to say wonderful blog!

  11. Jamesdiure表示:

    вавада казино онлайн: vavada kazi – вавада

  12. DarioTob表示:

    пинап казино: pinup – pinup

  13. Jamesdiure表示:

    пин ап казино официальный сайт: пин ап вход – pinup

  14. I think the admin of this website is truly working
    hard in support of his website, as here every material
    is quality based data.

  15. mostbetazyhSi表示:

    Mostbet-da o‘yin o‘ynash imkoniyati har doim mavjud | Mostbet mobil ilovasi bilan istalgan joyda o‘yinlardan bahramand bo‘ling | Mostbet onlayn kazinosi bilan o‘yin va g‘alaba yo‘lida keting | Mostbet bilan yuqori koeffitsiyentlar va katta yutuqlar oling | Mostbet-da yangi o‘yinlarni sinab ko‘ring va katta yutuqlar qo‘lga kiriting mostbet o’yinlari

  16. Josephmax表示:

    http://pinup-kazi.ru/# пин ап казино официальный сайт

  17. CurtisIncix表示:

    пин ап казино: пин ап казино – pinup-kazi.kz

  18. mostbetazneSi表示:

    Mostbet mobil ilovasini yuklab oling va o‘yinni boshlang | Mostbet yangi foydalanuvchilar uchun bonuslar taklif etadi | Mostbet orqali har kuni yangi aksiyalar va bonuslar | Mostbet-da o‘yin va pul tikish imkoniyatlaridan foydalaning | Mostbet-da yangi o‘yinlarni sinab ko‘ring va katta yutuqlar qo‘lga kiriting mostbetazcasino.com.az

  19. HenrydeT表示:

    казино вавада вавада казино зеркало казино вавада

  20. excellent post, very informative. I ponder why the opposite experts of this sector do not understand this.

    You should continue your writing. I’m sure, you have a huge readers’ base already!

  21. DarioTob表示:

    vavada: вавада казино онлайн – вавада казино зеркало

  22. phim sex表示:

    I am actually grateful to the owner of this site who has shared this great paragraph at
    at this place.

發佈留言

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