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,481,149 Responses

  1. Get the facts表示:

    Usually I don’t read post on blogs, but I wish to say that this write-up very compelled
    me to try and do it! Your writing style has been amazed
    me. Thanks, quite nice post.

  2. Hi! Do you know if they make any plugins to
    help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results.
    If you know of any please share. Appreciate it!

  3. Nikefut表示:

    tor darknet darknet drugs tor marketplace

  4. webcam drag queens表示:

    webcam online

  5. It’s a pity 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 fresh updates
    and will talk about this website with my Facebook group.
    Chat soon!

  6. See What Double Chaise Sofa Tricks The Celebs Are Utilizing double chaise sofa

  7. In diesem Artikel stellen wir eine Auswahl von TikTok Downloader ohne Wasserzeichen vor,
    die das Herunterladen in klarer und unmarkierter Form ermöglichen.

  8. The 10 Most Scariest Things About Best Accident Lawyers Near Me Accident Lawyers Near Me

  9. Very rapidly this web site will be famous amid all blog viewers,
    due to it’s nice content

  10. вывод из запоя в стационаре воронежа https://www.motik13.0pk.me/viewtopic.php?id=2001 .

  11. HiOP表示:

    After Hours HiOP

  12. If you’re considering a roof upgrade, have you thought of energy-efficient alternatives? It can help reduce your expenses! I learned a lot from checking out articles on roofer near me relating to environmentally friendly roofing options

  13. вывод из запоя в стационаре анонимно http://zal.rolevaya.info/viewtopic.php?id=5384/ .

  14. Montcalm County for a few years.

  15. лечение наркозависимости в стационаре motik13.0pk.me/viewtopic.php?id=2001 .

  16. budal表示:

    Sensual Massage budal

  17. ai-porn.tokyo表示:

    Greetings! Very helpful advice in this particular article!
    It is the little changes which will make the largest
    changes. Many thanks for sharing!

  18. When I originally commented I seem to have clicked the -Notify me when new
    comments are added- checkbox and now whenever
    a comment is added I receive four emails with the exact same comment.
    Is there a way you are able to remove me from that
    service? Appreciate it!

  19. Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is important and all. But imagine if you
    added some great visuals or videos to give your posts more, “pop”!
    Your content is excellent but with images and video clips, this site could
    definitely be one of the very best in its field. Superb blog!

  20. Heya i’m for the primary time here. I came across this board and I find It really helpful & it helped me out much.

    I am hoping to offer one thing again and help others like you helped me.

  21. I savour, result in I discovered exactly what I used to be taking
    a look for. You’ve ended my four day lengthy hunt! God Bless you man. Have a nice day.

    Bye

  22. Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is valuable and everything.
    But imagine if you added some great graphics or video clips
    to give your posts more, “pop”! Your content
    is excellent but with images and clips, this website could
    undeniably be one of the very best in its niche.
    Superb blog!

發佈留言

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