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...

874,277 Responses

  1. Genuine HGH Perks (NewULife Hgh Gel Not Evaluated Yet).

    My blog post rhodium price chart kitco

  2. 10 Websites To Help You Learn To Be An Expert In Malpractice
    Attorneys malpractice law Firm

  3. 5 Tools Everyone Is In The Slot Machines
    Industry Should Be Utilizing legitimate Slots

  4. MarvinRoave表示:

    buy amoxicillin 500mg online amoxicillin 875 125 mg tab buy amoxicillin 500mg capsules uk

  5. Undeniably believe that which you stated. Your favorite reason seemed to be
    on the web the easiest thing to be aware of. I say to you, I definitely get annoyed while
    people consider worries that they plainly do
    not know about. You managed to hit the nail upon the top
    and also defined out the whole thing without having side-effects , people
    can take a signal. Will probably be back to get more.
    Thanks

  6. Guide To Robot Vacuum For Pet Hair: The Intermediate Guide
    For Robot Vacuum For Pet Hair robot vacuum for pet hair (https://qooh.me/crowdbrass85)

  7. Billyved表示:

    http://gabapentinneurontin.pro/# brand name neurontin price

  8. Richardorele表示:

    amoxil generic: buy amoxil – over the counter amoxicillin

  9. What Is Planet Spa Pillow Mist? What Are The Benefits And How To Utilize It avon planet spa radiant gold

  10. MarvinRoave表示:

    neurontin 100 neurontin 2018 buy cheap neurontin online

  11. 10 Myths Your Boss Is Spreading Regarding Private ADHD Assessment Cost
    how Much does a private adhd assessment cost

  12. notabug.Org表示:

    10 Facts About Locksmith Car That Insists On Putting You In The Best Mood Mobile car key locksmith near me (notabug.Org)

  13. MarvinRoave表示:

    prednisone 1 tablet prednisone 50mg cost purchase prednisone

  14. 10 Things People Hate About Railroad Injuries Attorneys Thibodaux Railroad Injuries Attorney

  15. Wow, amazing weblog structure! How lengthy have you
    ever been running a blog for? you made blogging
    look easy. The entire look of your website
    is wonderful, as smartly as the content material!

  16. See What Dreame Bot L30 Ultra Tricks The Celebs Are Using dreame bot l30 ultra

  17. Billyved表示:

    http://gabapentinneurontin.pro/# neurontin prices generic

發佈留言

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