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,678,673 Responses

  1. DevonPhack表示:

    https://mexicanpharm24.cheap/# reputable mexican pharmacies online
    ed men

  2. Medicament information sheet. Generic Name.
    where can i buy cheap inderal without dr prescription
    Actual news about drug. Read information here.

  3. Williamagric表示:

    cheap erectile dysfunction pill http://mexicanpharm24.cheap/# buying prescription drugs in mexico online

  4. I’m gone to convey my little brother, that he should also visit this weblog on regular basis to get updated from most up-to-date reports.

    Look at my web page สล็อตเกมยิงปลา

  5. 5 Qualities People Are Looking For In Every Skoda Car
    Key Replacement skoda fabia Locked keys in car

  6. bokep jepang表示:

    You really make it seem so easy with your presentation but I find this matter to be really something
    which I think I would never understand. It seems too
    complex and extremely broad for me. I am looking forward for
    your next post, I’ll try to get the hang of it!

  7. zavstyle表示:

    You could certainly see your enthusiasm in the work
    you write. The world hopes for more passionate writers like you who
    aren’t afraid to mention how they believe. At all
    times go after your heart.

  8. LeroyBak表示:

    new erectile dysfunction treatment low cost pharmacy canadian drugs online

  9. Ten Things You’ve Learned In Kindergarden That Will Aid You In Obtaining Car Key
    Fob Repair key fob repairs near Me

  10. Nine Things That Your Parent Taught You About ADHD Diagnosis
    UK Adults adhd diagnosis Uk adults (justbookmark.win)

  11. Do you mind if I quote a couple of your articles as long as
    I provide credit and sources back to your site? My blog site is
    in the very same niche as yours and my users would really benefit from a
    lot of the information you provide here. Please let me know if this okay with you.

    Cheers!

  12. Kandace表示:

    You’ll Never Guess This Small L Shaped Couch’s Benefits small
    l shaped couch (Kandace)

  13. This is my first time pay a quick visit at here and
    i am genuinely pleassant to read all at alone place.

    Feel free to surf to my web-site … คาสิโนออนไลน์ ภาษาอังกฤษ

  14. Thankfulness to my father who informed me concerning this
    website, this weblog is in fact remarkable.

  15. Appreciating the persistence you put into your site and detailed information you present.
    It’s awesome to come across a blog every once in a while
    that isn’t the same unwanted rehashed material. Great read!
    I’ve saved your site and I’m including your RSS feeds
    to my Google account.
    https://paitohk6d.city/

  16. DevonPhack表示:

    http://pharm24.pro/# erectyle disfunction
    ed symptoms

  17. BrandonSaisy表示:

    pharmacy website india: Pharmacies in India that ship to USA – buy medicines online in india

  18. Repairing表示:

    The Next Big Event In The Mini Cooper Car Key Replacement Industry Repairing

  19. custom socks表示:

    I go to see each day a few websites and information sites to read posts, but this webpage presents quality
    based articles.

  20. вывод из запоя анонимно http://vyvod-iz-zapoya-moskva11.ru/ .

  21. вывод из запоя в стационаре москваа https://nazalnyj.ru .

  22. вывод из запоя на дому круглосуточно москва вывод из запоя на дому круглосуточно москва .

  23. LeroyBak表示:

    top online pharmacy india Best online Indian pharmacy indian pharmacy

  24. BrandonSaisy表示:

    mexico drug stores pharmacies: mexican drugs – mexican border pharmacies shipping to usa

發佈留言

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