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,211,093 Responses

  1. Nelsonbluck表示:

    buying prescription drugs in mexico: buying from online mexican pharmacy – reputable mexican pharmacies online

  2. DominicJaw表示:

    п»їbest mexican online pharmacies: purple pharmacy mexico price list – mexican rx online

  3. hi!,I love your writing so a lot! percentage we keep in touch more about your post on AOL?
    I require a specialist on this area to resolve my problem.
    May be that’s you! Looking ahead to look
    you.

  4. WayneCoirl表示:

    medication from mexico pharmacy: medication from mexico pharmacy – mexican pharmaceuticals online

  5. Arnoldten表示:

    mexico pharmacy mexico drug stores pharmacies buying prescription drugs in mexico online

  6. WayneCoirl表示:

    п»їbest mexican online pharmacies: buying from online mexican pharmacy – п»їbest mexican online pharmacies

  7. Arnoldten表示:

    buying prescription drugs in mexico medication from mexico pharmacy buying prescription drugs in mexico

  8. Nelsonbluck表示:

    buying prescription drugs in mexico online: mexican rx online – mexico drug stores pharmacies

  9. هرزه表示:

    Hello, i think that i saw you visited my web site so i came to “return the favor”.I am attempting to find things
    to improve my web site!I suppose its ok to use some of your
    ideas!!

  10. DominicJaw表示:

    mexico pharmacies prescription drugs: buying prescription drugs in mexico online – mexican pharmaceuticals online

  11. What’s The Current Job Market For Window Handle Replacement Professionals Like?
    window handle replacement

  12. https://drbo1b.z1.web.core.windows.net/research/drbo1b-(328).html
    A little sparkle is a must when celebrating such a joyous occasion.

  13. Three Reasons Why You’re Motorcycle Accident Attorneys Is Broken (And How
    To Repair It) motorcycle accidents (clicavisos.com.ar)

  14. DominicJaw表示:

    buying prescription drugs in mexico: pharmacies in mexico that ship to usa – mexican online pharmacies prescription drugs

  15. fuck表示:

    I am really enjoying the theme/design of your weblog.
    Ɗo you ever run intgo any wweb browser compatibility problems?
    A handfսl of my blog reаders have complaіned about my site not w᧐rking correctly
    in Expⅼorer Ƅut looks great in Firefox. Dⲟ yyou have ɑny advice
    to help fix this problem?

    my ѕite fuck

  16. Nelsonbluck表示:

    mexican mail order pharmacies: mexico pharmacies prescription drugs – buying from online mexican pharmacy

  17. A Look At The Ugly The Truth About Window.Glass Replacement window glass panel replacement

  18. 新竹 推拿表示:

    https://bogin017.netlify.app/research/bogin017-(226)
    So earlier than I even go into detail mother of the bride outfit ideas, I want to emphasize one essential factor.

  19. 10 Reasons That People Are Hateful Of High Loft Bed Childrens loft bed

  20. Great weblog here! Additionally your web site quite a bit up
    very fast! What web host are you the usage of? Can I am getting your affiliate hyperlink in your host?
    I wish my site loaded up as fast as yours lol

  21. WayneCoirl表示:

    medicine in mexico pharmacies: reputable mexican pharmacies online – mexican border pharmacies shipping to usa

  22. DominicJaw表示:

    pharmacies in mexico that ship to usa: mexico drug stores pharmacies – buying prescription drugs in mexico online

  23. Nelsonbluck表示:

    п»їbest mexican online pharmacies: medicine in mexico pharmacies – medicine in mexico pharmacies

  24. 5 Killer Quora Answers On Sugar Rush sugar rush Slot demo

  25. 台中 撥筋表示:

    https://bogin006.netlify.app/research/bogin006-(293)
    An occasion as special as your kid’s wedding doesn’t come around daily.

  26. The 10 Scariest Things About Auto Accident Attorneys auto accident attorneys

  27. The Reasons You Should Experience Replacement Window
    Handle At The Very Least Once In Your Lifetime Glass Window replacement near me

發佈留言

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