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,477,634 Responses

  1. выведение из запоя воронеж стационар http://www.zarabotokdoma.creartuforo.com/viewtopic.php?id=11483 .

  2. вывод из запоя в стационаре анонимно http://ideya.forums.party/viewtopic.php?id=661 .

  3. вывод из запоя в стационаре анонимно obovsem.rolevaya.info/viewtopic.php?id=3681 .

  4. Hi! I know this is sort of off-topic however I
    needed to ask. Does building a well-established website such as yours require a massive amount work?

    I’m completely new to running a blog but I do write in my diary daily.
    I’d like to start a blog so I can easily share my own experience
    and thoughts online. Please let me know if you have any kind of ideas or tips for new aspiring bloggers.
    Appreciate it!

  5. Elijah Perez表示:

    Only mounted beautiful textured backsplash tiles– incredible just how they increase home kitchen looks quickly! # # anyKey term flooring warehouse

  6. DarrenUnjuh表示:

    http://plavixclo.com/# cheap plavix antiplatelet drug

  7. Medicament information. Brand names.
    hydroxyzine pharmacodynamics
    Actual trends of medicament. Read information now.

  8. Drugs information sheet. What side effects?
    order generic chlorpromazine without rx
    Actual about medicine. Get information here.

  9. AlbertDuM表示:

    minocycline 50mg tablets online: iver fast – ivermectin 5

  10. Are You Responsible For A High Quality Coffee Beans Budget?
    12 Tips On How To Spend Your Money Premium Coffee Beans

  11. Highly descriptive article, I enjoyed that a lot.

    Will there be a part 2?

  12. вывод из запоя в стационаре анонимно https://gaslo.ukrbb.net/viewtopic.php?f=13&t=3413 .

  13. вывод из запоя в стационаре воронежа https://www.rolandus.org/forum/viewtopic.php?p=106439/ .

  14. BernardMop表示:

    stromectol ivermectin tablets buy Stromectol order minocycline 100mg online

  15. He is obtainable now and after being on our radar since he performed us, we are delighted to get him in.

  16. n8 casino表示:

    Hi, I think your site might be having internet browser compatibility problems.

    When I take a look at your blog in Safari, it looks fine however, if opening in IE, it has some
    overlapping issues. I just wanted to give you a quick heads
    up! Apart from that, wonderful blog!

  17. Raja Paito表示:

    It’s a shame you don’t have a donate button! I’d certainly donate to this
    fantastic blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account.

    I look forward to brand new updates and will share this website with my Facebook group.
    Chat soon!

    https://ww7.datawarnahk.club/

發佈留言

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