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,246,138 Responses

  1. Hello there! I simply would like to offer you a big thumbs up for your great info you have
    here on this post. I will be returning to your site for more
    soon.

  2. Han Pawlik表示:

    Oil Palm plantation still in demand

  3. This page really has all of the information I wanted about this subject and didn’t know who to ask.

  4. This was very beneficial. For more, visit MK Sports

  5. AlbertDuM表示:

    lisinopril1st: Lisinopril 1st – Lisinopril 1st

  6. Nikefut表示:

    darknet sites tor market links https://darknetmarketstore.com/

  7. BrandenSlomy表示:

    prednisone drug costs: Prednisone Without Prescription – prednisone 5mg coupon

  8. Markdob表示:

    Приветствую. Подскажите, где почитатьразные блоги о недвижимости? Пока нашел – https://oookuper.ru

  9. Markdob表示:

    Приветствую. Подскажите, где почитатьразные статьи о недвижимости? Пока нашел – https://oookuper.ru

  10. Irene King表示:

    The link between effective security procedures security guard service

  11. вывод из запоя анонимно краснодар https://chesskomi.borda.ru/?1-8-0-00003046-000-0-0-1730745020 .

  12. Markdob表示:

    Приветствую. Подскажите, где почитатьразные статьи о недвижимости? Сейчас читаю – https://oookuper.ru

  13. sdsplusby表示:

    Why users still make use of to read news papers when in this technological globe everything is available on net?

  14. BernardMop表示:

    Plavix 75 mg price [url=http://plavixclo.com/#]PlavixClo Best Price[/url] generic plavix

  15. DerekWab表示:

    кухни на заказ недорого спб – это функциональность и комфорт по разумной цене.

  16. Markdob表示:

    Приветствую. Подскажите, где найтиполезные статьи о недвижимости? Пока нашел – https://oookuper.ru

  17. wps office表示:

    Thank you for the good writeup. It in fact
    used to be a amusement account it. Look complex to more delivered agreeable from you!
    By the way, how can we keep in touch?

  18. Узнай все о выпрямление носовой перегородкиискривление носовой перегородки операция цена

  19. pp slot表示:

    Its not my first time to visit this website, i am browsing this
    website dailly and take pleasant data from here everyday.
    pp slot

  20. certainly like your website however you have to take a look at the spelling on quite a few of your posts.
    Several of them are rife with spelling problems and I in finding it very bothersome to tell the reality then again I will surely
    come again again.

  21. Узнай все о септопластика сколько стоитсептопластика носа цены в москве

發佈留言

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