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,227 Responses

  1. nodeposit表示:

    Hi colleagues, how is the whole thing, and what you
    wish for to say about this paragraph, in my view its genuinely awesome for me.

  2. Do you have any video of that? I’d care to find out more details.

    Feel free to surf to my homepage realtor in Meridian Ms

  3. Hi it’s me, I am also visiting this website daily, this site is
    truly fastidious and the users are in fact sharing pleasant thoughts.

  4. CurtisIncix表示:

    пин ап казино: pinup – пин ап казино онлайн

  5. Pretty nice post. I just stumbled upon your blog and wished to
    mention that I’ve really enjoyed browsing your blog posts.
    After all I’ll be subscribing to your feed and I am hoping you write again very soon!

    Stop by my site – realtor in volusia county fl

  6. DarioTob表示:

    казино вавада: vavada-kazi.ru – вавада казино

  7. Great blog! Is your theme custom made or did you
    download it from somewhere? A theme like yours with a few simple tweeks would really make my blog shine.
    Please let me know where you got your design. Thank you

  8. Medicament prescribing information. Effects of Drug Abuse.
    metformin 500mg tablets price
    Actual information about medicine. Read now.

  9. Josephmax表示:

    http://pinup-kazi.ru/# пин ап зеркало

  10. CurtisIncix表示:

    пин ап казино официальный сайт: пин ап казино – pinup-kazi.ru

  11. Vediamo, in questa nuova sezione, quali sono gli altri bonus che possono
    essere riscattati nei casino online stranieri.

  12. Greetings! I know this is somewhat off topic but I was wondering
    which blog platform are you using for this site? I’m getting tired of WordPress because I’ve had
    problems with hackers and I’m looking at options for another platform.

    I would be awesome if you could point me best real estate agent in tucson az the direction of a good platform.

  13. Jamesdiure表示:

    пин ап казино официальный сайт: пинап казино – пин ап казино официальный сайт

  14. I visited many websites except the audio quality for audio songs present
    at this website is really excellent.

    Here is my web-site; best realtor in tucson arizone

  15. Josephmax表示:

    https://vavada-kazi.ru/# казино вавада

  16. DarioTob表示:

    пин ап вход: пин ап зеркало – pinup

  17. you are truly a excellent webmaster. The site loading pace is incredible.

    It seems that you’re doing any distinctive trick.
    Moreover, The contents are masterpiece. you’ve done
    a wonderful process in this matter!

  18. Meds information sheet. Generic Name.
    order cheap minocycline without prescription
    Best what you want to know about pills. Get information here.

  19. Jamesdiure表示:

    вавада казино зеркало: вавада казино онлайн – вавада казино онлайн

  20. DarioTob表示:

    pinup-kazi.ru: пин ап казино – пин ап вход

  21. Attending a prove at a Las Vegas strip club need to be on all of us’s bucket checklist! Learn extra approximately it at Las Vegas Private Strippers

  22. Excellent blog you’ve got here.. It’s hard to find high-quality writing like yours
    nowadays. I truly appreciate people like you! Take
    care!!

發佈留言

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