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,368,341 Responses

  1. you are actually a good webmaster. The site loading velocity is incredible.
    It seems that you’re doing any distinctive
    trick. Also, The contents are masterpiece. you’ve done a excellent task on this matter!

  2. электрические гардины для штор электрические гардины для штор .

  3. Excellent way of describing, and nice paragraph to obtain data
    on the topic of my presentation topic, which i am going to present
    in academy.

  4. StevenNub表示:

    over the counter amoxicillin canada: cheap amoxil – amoxicillin 500mg capsules uk

  5. карнизы с электроприводом купить карнизы с электроприводом купить .

  6. Have you ever considered publishing an e-book or guest authoring on other websites?
    I have a blog centered on the same topics you discuss and
    would love to have you share some stories/information. I know
    my subscribers would value your work. If you’re even remotely interested, feel
    free to send me an e-mail.

  7. электрический карниз для штор электрический карниз для штор .

  8. StevenNub表示:

    how to purchase prednisone online: prednisoneraypharm – prednisone pills 10 mg

  9. How Coffee Machine Delonghi Became The Hottest Trend Of 2023 Coffee Machine Offers

  10. BrandenSlomy表示:

    dapoxetine price: priligy max pharm – Priligy tablets

  11. Terrific work! That is the type of information that should be shared around the web.
    Shame on the search engines for now not positioning this submit upper!

    Come on over and talk over with my website .
    Thank you =)

  12. BrandenSlomy表示:

    priligy maxpharm: cheap priligy – priligy maxpharm

  13. Markdob表示:

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

  14. Why visitors still make use of to read news papers when in this technological world everything is
    presented on net?

  15. StevenNub表示:

    dapoxetine price: max pharm – cheap priligy

  16. Markdob表示:

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

  17. Nikefut表示:

    blackweb darknet drug links https://darknetmarketstore.com/ – deep web search

  18. pepek表示:

    Having read this I thought it was rather informative.

    I appreciate you taking the time and effort to put this
    short article together. I once again find myself personally spending a lot of
    time both reading and posting comments. But so what, it was still worth
    it!

  19. Markdob表示:

    Всем привет! Может кто знает, где найтиполезные блоги о недвижимости? Пока нашел – https://glavtorgmsk.ru

  20. I not too long ago partnered with a virtual advertising and marketing service provider, and the consequences were surprising! If you’re in search of expert assist, money out Local SEO for a few sizeable insights

發佈留言

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