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,466,930 Responses

  1. locksmith表示:

    What’s The Job Market For Best Car Locksmith Near Buckinghamshire Professionals
    Like? locksmith

  2. kopsus.pro表示:

    Tremendous things here. I am very glad to look your post.
    Thank you so much and I am looking forward to contact you.
    Will you kindly drop me a mail?

  3. Marcy表示:

    Greetings! I know this is kinda off topic but I was wondering which blog platform
    are you using for this website? I’m getting fed up of WordPress because I’ve
    had problems with hackers and I’m looking at alternatives for another platform.
    I would be awesome if you could point me in the direction of a good platform.

  4. What’s up colleagues, pleasant article and nice urging
    commented at this place, I am genuinely enjoying
    by these.

  5. Thanks for the thorough article. Find more at roofers greenville sc

  6. Aaron Reese表示:

    Fantastic information on security fencing! I’ll be visiting quality timber fencing services in Melbourne soon

  7. 苗栗外燴表示:

    https://objectstorage.ap-tokyo-1.oraclecloud.com/n/nrswdvazxa8j/b/digi176sa/o/research/digi176sa-(218).html
    It’s out there in a range of colors, from “champagne” (off-white) to navy.

  8. BOKEP VIRAL表示:

    Hello all, here every person is sharing such experience, so it’s pleasant to
    read this website, and I used to pay a visit this blog every day.

  9. Shaneevige表示:

    Attractive section of content. I just stumbled upon your site and in accession capital to assert that I acquire actually enjoyed account your blog posts. Anyway I’ll be subscribing to your augment and even I achievement you access consistently rapidly.
    https://millionigrushek.ru/

  10. Wallacefex表示:

    en cok kazand?ran slot oyunlar?: slot casino siteleri – slot oyunlar? puf noktalar?

  11. Leroy Newton表示:

    Mold subject matters is usually a serious quandary for house owners in Lehi, extraordinarily with the zone’s varying humidity stages. It’s principal to address any indicators of mildew development right now to verify a fit residing ambiance lehi mold remediation

  12. Celia Fuller表示:

    Is it safe to combine different treatments during one visit to a medical spa? Looking for some guidance here! Lotus Health and Aesthetics

  13. Cynthia Nunez表示:

    Bedankt voor het delen van deze inzichten over gebitsproblemen en hoe ze voorkomen kunnen worden! factoren die mondgezondheid beïnvloeden

  14. It’s a pity you don’t have a donate button! I’d definitely donate to this brilliant blog!
    I suppose for now i’ll settle for bookmarking and
    adding your RSS feed to my Google account. I look forward to brand new
    updates and will share this site with my Facebook group.
    Chat soon!

  15. OP表示:

    Stress Relief – Top Tips OP

  16. Lettie Hansen表示:

    Water destroy will be a owner of a house’s worst nightmare, mainly in places like Long Beach where flooding can occur all of a sudden. It’s principal to behave quick to cut back destroy and repair your private home water damage restoration long beach

  17. FrankEmine表示:

    yat?r?ms?z deneme bonusu veren siteler: deneme bonusu veren siteler yeni – denemebonusuverensiteler25

  18. Water hurt will also be a homeowner’s worst nightmare, quite in parts like Palmdale, CA, where the local weather can result in sudden leaks and flooding. It’s primary to deal with these issues right away to evade additional damage https://maps.app.goo.gl/QN4fqHgZopoZu2o47

  19. Seth Frazier表示:

    If you’re hunting for cost effective ideas for SR22 insurance plan in Florida, you’re not by myself! Many drivers are looking ways to fulfill their insurance coverage specifications without breaking the bank https://maps.app.goo.gl/83JRMmtus7RHwHzk7

  20. Logan Drake表示:

    Well explained. Discover more at Shelbyville IN roofers

  21. Marion McGee表示:

    I appreciate that many medical spas provide educational resources about their treatments online! medical spa treatment

  22. BradleyEdino表示:

    deneme bonusu veren siteler yeni deneme bonusu veren yeni siteler deneme bonusu veren yeni siteler

  23. Martha Carson表示:

    If you might be seeking reasonably-priced SR22 insurance coverage in Florida, it is necessary to compare the different vendors to to find the preferable premiums. Many worker’s are shocked at how a great deal they could shop by way of looking around https://maps.app.goo.gl/DHkwsm9CiBYk8oZt9

  24. KELUARAN SDY表示:

    Way cool! Some very valid points! I appreciate you writing this post and also the rest of the site is also very good.

  25. Betty Atkins表示:

    Nicely detailed. Discover more at pool remodel near me

  26. JasonUnsoG表示:

    https://casinositeleri25.com/# Deneme Bonusu Veren Siteler

  27. Zolin Tex表示:

    Just wish to say your article is as amazing. The clearness to your put up is just spectacular and that i could suppose you’re an expert in this subject.

    Well along with your permission let me to grasp your RSS feed to keep up to date with coming near near post.
    Thank you one million and please continue the
    rewarding work.

發佈留言

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