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,366,396 Responses

  1. StevenNub表示:

    dapoxetine online: dapoxetine online – buy priligy

  2. StevenNub表示:

    where buy generic clomid without a prescription: can i buy cheap clomid online – get clomid online

  3. online表示:

    Hey, I think your website might be having browser compatibility issues.
    When I look at your blog site in Opera, it looks fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to give you a quick heads up!
    Other then that, superb blog!

  4. StevenNub表示:

    where to buy amoxicillin over the counter: Amoxicillin buy online – buy cheap amoxicillin online

  5. Aproveite as mais abrangentes interpretacoes.
    Porque e que sonho com o kvass?

    [url=http://rukids.co.kr/bbs/board.php?bo_table=review&wr_id=869120]Olhar num espelho n[/url] [url=https://www.iochatto.com/storytlr-la-tua-vita-online/2828/comment-page-11415/#comment-6120219]Com que e que um elefante sonha?[/url] [url=https://www.bostoncouplestherapy.com/ultimate-guide-couples-therapy/#comment-36312]Olhar num espelho num sonho[/url] [url=http://www.uttsb.com.my/hello-world/#comment-934271]Com que e que um elefante sonha?[/url] [url=http://nrp.i7.lt/index.php?/topic/982813-%D1%86%D0%B8%D1%84%D1%80%D0%BE%D0%B2%D1%8B%D0%B5-%D0%BA%D0%B0%D0%BC%D0%B5%D1%80%D1%8B-%D0%B4%D0%BB%D1%8F-%D0%BC%D0%B8%D0%BA%D1%80%D0%BE%D1%81%D0%BA%D0%BE%D0%BF%D0%B0/page-23#entry4034830]Sonho de loja de penhores[/url] 7c49793

  6. Dereketerm表示:

    priligy: buy priligy – priligy max pharm

  7. Markdob表示:

    Приветствую. Может кто знает, где найтиразные блоги о недвижимости? Сейчас читаю – https://helyx-perm.ru

  8. StevenNub表示:

    how can i get prednisone: prednisone cream rx – canadian online pharmacy prednisone

  9. Markdob表示:

    Приветствую. Может кто знает, где найтиразные статьи о недвижимости? Сейчас читаю – https://helyx-perm.ru

  10. Why viewers still use to read news papers when in this technological world all is presented on web?

    Feel free to visit my web page – stem cell treatment

  11. WengToto表示:

    WOW jսst what I ᴡas searching for. Came һere Ƅy searching
    ffor live news fox

    Loook іnto my website …WengToto

  12. Nancee表示:

    Look into my web page Daycares popular listings (Nancee)

  13. scam表示:

    Excellent content With thanks.

  14. StevenNub表示:

    cost clomid price: cheap clomid – how to get generic clomid without prescription

  15. Choose a smarter document solution with Scanner.biz.
    Today’s fast pace calls for efficient document scanning and
    organization tools. Effortlessly create high-quality PDFs and JPGs from paper documents with Scanner.biz.
    One-tap scanning for notes to contracts, with tools for cropping and adding signatures.

    Scanner.biz offers OCR, making scanned images editable for users.

    Upload files to cloud platforms like Google Drive
    and Evernote for easy access. A flexible solution for
    solo users, students, and teams, Scanner.biz keeps documents secure.
    Ditch traditional scanners for seamless mobile document management
    with Scanner.biz.

  16. Excellent beat ! I would like to apprentice even as you amend your web
    site, how can i subscribe for a weblog web site?
    The account aided me a acceptable deal. I have been tiny bit acquainted of this your
    broadcast provided shiny clear concept

    my web-site Pinoy SEO Services

  17. video chat表示:

    I do not even know how I ended up here, but I thought this post was
    great. I do not know who you are but definitely you’re going to a famous
    blogger if you aren’t already 😉 Cheers!

    My site video chat

  18. Cricket Id表示:

    If you’re looking for a great betting experience, Online Cricket ID is the way to go! The platform’s focus on sports and card games provides endless entertainment. Registration and login are super quick, giving you instant access to all your favorite games. Plus, the game quality and reliable services make it a great choice for secure online betting. Worth a try for any game lover!

  19. Most Popular Apps to Make Money in Pakistan, For Extra Income, That Will Change Your Financial Life, Optimal Apps to Make Money in Pakistan, Top Ways to Make Money in Pakistan through mobile applications, which have already been rated by thousands of users, which you need to know, Modern ways to earn money in Pakistan through applications, How to make money, without leaving home in Pakistan, to increase financial flow, Accurate methods of making money in Pakistan, with minimal effort and maximum return, to increase income, Easy ways to make money in Pakistan through apps, with guaranteed payments, The easiest apps for making money in Pakistan, which bring real money, which will open up new opportunities for earning moneyonline earning app in pakistan real earning apps in pakistan .

發佈留言

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