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,130,043 Responses

  1. Drstobeese表示:

    cialis viagra online pharmacy: uk pharmacy viagra online – ed meds online

  2. Bookmarks表示:

    Arizona has one district for bankruptcy filings.

    my web site Bookmarks

  3. There are infantile spasms painful a number
    of sources of infantile spasms.

  4. Easydrorbix表示:

    fred’s dollar store pharmacy: boots pharmacy doxycycline – fry’s food store pharmacy hours

  5. I have to thank you for the efforts you have put in writing this site.

    I really hope to check out the same high-grade content by you in the future as well.
    In truth, your creative writing abilities has motivated me to get my very own website now ;
    )

  6. Pod And Capsule Coffee Machines 101 This Is
    The Ultimate Guide For Beginners Capsule coffee Makers

  7. Wow! This blog lօoks just lіke my οld one! It’s on а completely differrnt subject butt іt hɑs pretty mսch
    tһe ѕame lagout and design. Superb choice ߋf colors!

    Feel free tо viusit my рage … slot online gacor

  8. XYGENYX, a licensing company for FDA-registered products.

    Feel free to surf to my site; Orthopedic Surgeon Specialist

  9. Drstobeese表示:

    top online pharmacy 247: erythromycin online pharmacy – online pharmacy free viagra samples

  10. Если вы искали где отремонтировать сломаную технику, обратите внимание – профи услуги

  11. Если вы искали где отремонтировать сломаную технику, обратите внимание – сервис центр в челябинске

  12. memek表示:

    You need to be a part of a contest for one of the highest quality sites online.

    I am going to highly recommend this site!

  13. Easydrorbix表示:

    wholesale pharmacy online: zyprexa prices pharmacy – best viagra pharmacy

  14. Audi New Key表示:

    What You Can Do To Get More Out Of Your Audi Spare Key Audi New Key

  15. Если вы искали где отремонтировать сломаную технику, обратите внимание – сервисный центр в челябинске

  16. 10 Undeniable Reasons People Hate Folding Treadmill Fold Up Treadmill

  17. 8 Tips To Increase Your Hyundai Key Replacement Game Keyless entry hyundai

  18. Если вы искали где отремонтировать сломаную технику, обратите внимание – ремонт цифровой техники челябинск

  19. ton表示:

    I like the valuable info you provide in your articles.

    I will bookmark your weblog and test again right here frequently.
    I’m rather sure I’ll be told a lot of new stuff proper right here!
    Best of luck for the next!

  20. srasurvivor表示:

    I’m gone to say to my little brother, that he should also visit this blog on regular basis to take updated
    from hottest information.

  21. Easydrorbix表示:

    online generic pharmacy: online drug store – clozaril pharmacy registration

  22. Easydrorbix表示:

    online pharmacy degree programs: xenical singapore pharmacy – wedgewood pharmacy

  23. Existing law covers three lawful process.

    Feel free to visit my web site :: how much does a lawyer cost to file bankruptcy

  24. 9 Lessons Your Parents Teach You About Make Spare Car
    Key Make Spare car Key

  25. Easydrorbix表示:

    online pharmacy checker: united healthcare online pharmacy – cialis in indian pharmacy

  26. tgslot表示:

    I am really enjoying the theme/design of your web site.
    Do you ever run into any internet browser compatibility
    problems? A number of my blog visitors have complained about my site not operating correctly in Explorer but looks great in Chrome.
    Do you have any recommendations to help fix this problem?

  27. alpha bites表示:

    It’s perfect time to make a few plans for the longer term
    and it’s time to be happy. I have learn this post and if I may just I want to suggest you some interesting things
    or tips. Perhaps you can write next articles regarding this article.

    I want to read even more issues about it!

    My homepage – alpha bites

發佈留言

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