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,232,975 Responses

  1. Davidtes表示:

    buy generic ciprofloxacin buy cipro ciprofloxacin over the counter

  2. Upvc Repairs Near Me Tips To Relax Your Daily Lifethe One Upvc Repairs Near
    Me Trick That Everybody Should Be Able To upvc repairs near Me

  3. Hi there, just became alert to your blog through Google, and found that it’s
    truly informative. I am gonna watch out for brussels.
    I’ll be grateful if you continue this in future.
    Numerous people will be benefited from your writing. Cheers!

  4. If some one needs expert view regarding blogging afterward i suggest him/her to go to see this blog, Keep up the good job.

  5. What To Look For In The Right Designer Handbags Sale For
    You Designer Handbags Popular

  6. WilliamRhype表示:

    https://lisinopril.network/# cost for 2 mg lisinopril

  7. Davidtes表示:

    lisinopril 104 lisinopril 20 mg buy prices for lisinopril

  8. 10 Myths Your Boss Is Spreading Concerning New Avon Book avon book December 2023; http://daywell.kr/bbs/board.php?bo_table=free&wr_id=419125,

  9. 15 Secretly Funny People Work In Wood Burning Stoves Uk
    Heating Solutions

  10. RandallTaulp表示:

    cost propecia without rx: cost of propecia prices – cost of generic propecia without rx

  11. What Is Titration ADHD? History Of Titration ADHD
    what Is titration adhd

  12. accident表示:

    The Most Popular Semi Truck Claim The Gurus Are Using 3 Things accident

  13. lawyer表示:

    9 . What Your Parents Taught You About Medical Malpractice Lawyer lawyer

  14. Jacqueline表示:

    11 Ways To Completely Revamp Your Folding Treadmills
    treadmills fold up (Jacqueline)

  15. Ronaldpsymn表示:

    https://finasteride.store/# cost of cheap propecia prices

  16. Ronaldpsymn表示:

    http://ciprofloxacin.tech/# buy cipro online canada

  17. WilliamRhype表示:

    http://lisinopril.network/# lisinopril 5 mg price in india

  18. Ronaldpsymn表示:

    https://finasteride.store/# propecia tablets

  19. Davidtes表示:

    cost of cheap propecia tablets order propecia tablets order generic propecia without dr prescription

  20. PuffWow表示:

    Link exchange is nothing else but it is only placing the other person’s website link on your page at appropriate
    place and other person will also do same in support of you.

    Here is my blog post; PuffWow

  21. RandallTaulp表示:

    generic propecia: order cheap propecia price – get cheap propecia tablets

  22. What’s The Reason Everyone Is Talking About Double Glazed Window Repair This Moment window repairs near Me

  23. Davidtes表示:

    femara vs tamoxifen where can i buy nolvadex does tamoxifen cause weight loss

  24. I always used to study piece of writing in news papers but now as I am a user of internet
    thus from now I am using net for articles or reviews, thanks to web.

  25. WilliamRhype表示:

    http://ciprofloxacin.tech/# ciprofloxacin over the counter

  26. WilliamRhype表示:

    https://ciprofloxacin.tech/# ciprofloxacin mail online

  27. RandallTaulp表示:

    rx lisinopril 10mg: lisinopril 10 mg tablet price – zestril pill

  28. 5 Killer Quora Answers To Online Shopping Uk Women’s Clothing Online Shopping Uk Women’s Clothing (Spacebohemian.Com)

發佈留言

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