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,373,314 Responses

  1. Lorrie表示:

    Why Double Glazing Replacement Sealed Units Is Greater Dangerous Than You Think upvc window rubber seal replacement near me – Lorrie,

  2. amazon4d表示:

    Fabulous, what a web site it is! This webpage presents valuable
    information to us, keep it up.

  3. Hello! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying to
    get my blog to rank for some targeted keywords but I’m not seeing very good success.

    If you know of any please share. Many thanks!

  4. Carolyn Page表示:

    What are the best practices for grooming senior animals? I wish to make certain my older dog fits during the process mobile pet grooming

  5. kamagraeu表示:

    I’m curious to find out what blog system you are working
    with? I’m experiencing some minor security problems with
    my latest website and I would like to find something more safeguarded.
    Do you have any suggestions?

  6. Hilda Wilkins表示:

    The complete satisfaction of viewing dirt blast away during pressure washing is unmatched! If you concur https://qqpipi.com//index.php/Soft_Washing_vs._Pressure_Washing:_Which_Technique_is_Right_for_Your_Home%3F

  7. Thorough background checks ensure that only the best c Security guard service in Tucson

  8. Making informed decisions based upon reliable resources available locally enables homeowners maintain peace mind knowing support systems exist surrounding various challenges encountered regularly ! Utah Home Restoration

  9. Spend the first few driving lessons going over the basics of driving and cars.

  10. situs scam表示:

    constantly i used to read smaller articles that as well clear their motive, and
    that is also happening with this post which I am reading now.

  11. I am really grateful to the holder of this web page who has shared this great article at here. https://Cl-system.jp/question/flooring-is-always-first-2/

  12. Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your site?
    My website is in the very same area of interest as yours and my
    users would truly benefit from a lot of the information you present here.

    Please let me know if this ok with you. Appreciate it!

  13. How To Outsmart Your Boss On Toyota Car Key toyota key cutting near me

  14. AngelCroft表示:

    фотосъемка ювелирных изделий – Качественная фотосъемка ювелирных украшений, направленная на создание визуальной привлекательности.

  15. Marie Butler表示:

    The shift towards sustainability in branding is a hot topic – great to read about it here! Explore sustainable practices at digital marketing

  16. This website was… how do I say it? Relevant!! Finally I have found something which helped me.
    Thank you!

  17. выведение из запоя спб [url=https://mymoscow.forum24.ru/?1-1-0-00001727-000-0-0-1730832119]выведение из запоя спб [/url] .

  18. can i order cheap zithromax can you buy generic zithromax no prescription cheap zithromax online
    generic zithromax price where to buy generic zithromax for sale order zithromax pill
    cost cheap zithromax without insurance
    cheap zithromax tablets where to buy cheap zithromax tablets get zithromax pill
    order zithromax pill buying cheap zithromax without insurance where can i get zithromax without insurance

發佈留言

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