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,370,179 Responses

  1. KennethPah表示:

    buy cipro cheap ciprofloxacin order online п»їcipro generic

  2. DanielAbice表示:

    https://clomidonpharm.com/# order cheap clomid no prescription

  3. KennethPah表示:

    buy cheap amoxicillin Amox Star amoxicillin buy no prescription

  4. EdgarWat表示:

    Интересные статьи о займах – https://tnv.ru/news/economy/224210-vygodnye-mfo-dlya-predprinimateley/

  5. websell begok表示:

    Unquestionably believe that which you said. Your favorite justification appeared to be on the internet
    the easiest thing to be aware of. I say to you, I certainly get annoyed while people think about worries that they plainly don’t 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 likely be back to get more. Thanks

  6. Medication prescribing information. Effects of Drug Abuse.
    where to get reglan for sale
    All about medicine. Get information here.

  7. I’m not sure why but this website is loading extremely slow for
    me. Is anyone else having this issue or is it a problem on my end?
    I’ll check back later and see if the problem still exists.

  8. Terry Hodges表示:

    Traveling should be frustrating, but having a journey agent simplifies all the things! Learn about their reward at travel agency myrtle beach

  9. RobertJar表示:

    where can i buy cheap clomid without prescription: generic clomid tablets – where buy clomid

  10. DanielAbice表示:

    https://amoxstar.com/# amoxicillin 500 mg cost

  11. Medicine prescribing information. Short-Term Effects.
    can i order generic diflucan price
    Everything about drug. Read information here.

  12. Nikefut表示:

    free dark web darkmarket link darknet markets

  13. SamuelBix表示:

    how to buy amoxycillin: amoxil generic – amoxicillin 500 mg tablet

  14. KennethPah表示:

    how can i get clomid without rx can i get generic clomid without dr prescription how to get generic clomid without insurance

  15. The Best Folding Treadmill UK Tricks To Transform Your
    Life best Folding treadmill

  16. KennethPah表示:

    where to buy amoxicillin cost of amoxicillin prescription for amoxicillin

  17. Sex Education表示:

    I must thank you for the efforts you have put in penning this blog.
    I’m hoping to check out the same high-grade blog posts from you
    later on as well. In truth, your creative writing abilities has
    inspired me to get my own, personal site now 😉

  18. A friend highly recommended seeing a chiropractor after her accident Whiplash chiropractor near me

  19. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and
    was hoping maybe you would have some experience with something like this.

    Please let me know if you run into anything. I truly enjoy
    reading your blog and I look forward to your
    new updates.

  20. This website was… how do you say it? Relevant!! Finally I have
    found something which helped me. Cheers!

  21. EdgarWat表示:

    Интересные статьи о займах – http://rus-linux.net/press-reliz/2021-12/sravni.html

發佈留言

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