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,242,794 Responses

  1. Dereketerm表示:

    buy priligy max pharm: cheap priligy – cheap priligy

  2. StevenNub表示:

    priligy max pharm: priligy – max pharm

  3. This Is The Advanced Guide To Retro American Fridge Freezer Retro American Fridge Freezers (Istartw.Lineageinc.Com)

  4. Peculiar article, totally what I was looking for.

  5. What’s up, everything is going sound here and ofcourse
    every one is sharing facts, that’s really good, keep up writing.

  6. Тут можно преобрести сейф для оружия купить сейф для оружия купить москва

  7. BrandenSlomy表示:

    where to buy amoxicillin over the counter: com pharm – amoxicillin over the counter in canada

  8. BrandenSlomy表示:

    amoxicillin 500 mg without a prescription: cheap amoxil – medicine amoxicillin 500mg

  9. Тут можно преобрести сейфы под оружие купить сейф для карабина

  10. I think the admin of this website is genuinely working hard
    for his web site, since here every information is quality based information.

  11. BrandenSlomy表示:

    amoxicillin 500 mg where to buy: buy amoxicillin 500mg – how much is amoxicillin prescription

  12. Dereketerm表示:

    amoxicillin 500 mg brand name: cheap amoxil – amoxicillin medicine

  13. BrandenSlomy表示:

    amoxicillin 500: Amoxicillin buy online – where to buy amoxicillin pharmacy

  14. Your method of telling everything in this post is truly fastidious, every one be capable of easily be aware of it, Thanks a lot https://carryu.com/

  15. Hello, 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 web site!I suppose its ok to use some
    of your ideas!!

  16. Nikefut表示:

    dark web links darknet drug market best darknet markets

  17. Узнай все о варикоцеле 1 степени варикоцеле 3 степени

  18. Узнай все о степени варикоцеле двустороннее варикоцеле

  19. I visited various web sites except the audio quality
    for audio songs present at this web site is truly superb.

  20. Nice post. I learn something totally new and challenging on blogs I stumbleupon every day.
    It’s always helpful to read through articles from other authors and
    use something from other web sites.

  21. cammatch表示:

    I like the valuable info you provide in your articles.
    I’ll bookmark your weblog and check again here
    regularly. I’m quite certain I’ll learn plenty of new stuff right here!
    Best of luck for the next!

    Also visit my blog; cammatch

  22. I visited multiple websites except the audio quality
    for audio songs present at this site is in fact fabulous.

  23. Узнай все о варикоцеле яичка лапароскопия варикоцеле

  24. BrandenSlomy表示:

    buy dapoxetine online: buy priligy max pharm – buy priligy

  25. Узнай все о двустороннее варикоцеле признаки варикоцеле

  26. These are genuinely impressive ideas in on the topic of blogging.
    You have touched some pleasant factors here. Any way keep up
    wrinting.

  27. 20 Important Questions To ASK ABOUT Electric Stove Fireplace
    Before You Buy Electric Stove Fireplace buy electric stove

  28. Nikefut表示:

    darknet market lists dark websites deep web search

  29. Lazrucg表示:

    Полезная информация как купить диплом о высшем образовании без рисков
    iluzeia.flybb.ru/viewtopic.php?f=2&t=271

發佈留言

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