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,160,640 Responses

  1. Ten Cerebral Palsy Lawyers That Will Actually Make Your Life Better cerebral palsy lawsuit

  2. boy789th表示:

    Saved as a favorite, I love your website!

  3. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://salvatierrasostenible.es/esgtqb of type and scrambled it to make a type specimen book.

  4. If you are going for most excellent contents like me, simply
    pay a quick visit this web page daily because it gives quality contents,
    thanks

    Feel free to surf to my website – genius wave reviews

  5. 토토친구表示:

    Of course, commercial casinos represent only one type of gambling recognized by the American Gambling
    Association (AGA).

    my webpage; 토토친구

  6. See What Window Repairs Near Me Tricks The Celebs Are Making Use Of Window Repairs Near Me

  7. Medicines information. What side effects can this medication cause?
    where can i get generic kamagra prices
    Everything information about medicament. Get here.

  8. Geraldwaw表示:

    Virgil van Dijk https://virgilvandijk.prostoprosport-br.com Futebolista holandes, zagueiro central, capitao do clube ingles Liverpool e capitao do a selecao holandesa.

  9. Are You Getting The Most The Use Of Your Online Shopping Sites For Clothes?
    Shopping Online

  10. 20 Resources That Will Make You More Successful At ADHD Private Diagnosis Cost
    private Adhd assessment cost

  11. bokep smp表示:

    We’re a group of volunteers and opening a new scheme in our community.
    Your site offered us with valuable information to work
    on. You’ve done an impressive job and our entire community
    will be grateful to you.

  12. Dangerous Drugs Lawyer Tools To Help You Manage Your
    Daily Life Dangerous Drugs Lawyer Trick Every Individual Should Know Dangerous drugs Lawyer

  13. What is New U Life SOMADERM Transdermal Gel.

    Feel free to surf to my web page – type 1 diabetes disability benefits uk

  14. The IQ is the very best dry natural herb vegetable based vape juice I have actually attempted.

  15. Hey there just wanted to give you a quick heads up.
    The text in your article seem to be running off the screen in Ie.

    I’m not sure if this is a formatting issue or something to do with web
    browser compatibility but I figured I’d post to let you
    know. The design look great though! Hope you get the problem resolved
    soon. Many thanks

  16. How To Ship To Ireland From Uk Tools To Streamline Your Everyday Lifethe Only How To Ship To Ireland From
    Uk Trick Every Person Should Be Able To How To Ship To Ireland From Uk (Latenitetip.Com)

  17. l9U5qO表示:

    Wow! In the end I got a blog from where I be able to genuinely take valuable information regarding my
    study and knowledge.

  18. Drugs information sheet. What side effects?
    where to buy generic cipro prices
    Some what you want to know about drugs. Read now.

  19. memek becek表示:

    Whats up are using WordPress for your blog platform?
    I’m new to the blog world but I’m trying to get started and set up my own. Do you need any html coding knowledge to make your own blog?
    Any help would be greatly appreciated!

  20. Let me reveal you in my testimonial of NewULife.

    Have a look at my webpage; motion graphics online editing

  21. Pills information. Drug Class.
    where buy anastrozole pill
    Some news about medicament. Get information here.

  22. You have made some decent points there. I checked on the web for more info about
    the issue and found most individuals will go along with your views on this website.

  23. NewULife Ranks. Invite to my NewUlife Review.

    Look at my homepage; best Ai headshot generator reddit

  24. Fastidious respond in return of this issue with solid arguments and
    telling the whole thing on the topic of that.

  25. xhxh表示:

    I loved as much as you’ll receive carried out right here.

    The sketch is attractive, your authored material stylish.

    nonetheless, you command get bought an shakiness over that you wish be delivering the following.
    unwell unquestionably come more formerly again since exactly
    the same nearly a lot often inside case you shield this increase.

  26. Meds information for patients. Short-Term Effects.
    how can i get cheap nolvadex for sale
    Some trends of drug. Read information here.

發佈留言

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