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,372,993 Responses

  1. Mason Wilson表示:

    Thank you for losing light on the value of specialist locksmiths! Great paintings! locksmith la porte tx

  2. Hi just wanted to give you a brief heads up and let you know
    a few of the pictures aren’t loading correctly.
    I’m not sure why but I think its a linking issue. I’ve tried it in two different web
    browsers and both show the same results.

  3. DanielAbice表示:

    http://clomidonpharm.com/# where can i get cheap clomid price

  4. This design is spectacular! You most certainly know how to
    keep a reader entertained. Between your wit and your videos,
    I was almost moved to start my own blog (well, almost…HaHa!) Wonderful job.

    I really enjoyed what you had to say, and more than that,
    how you presented it. Too cool!

  5. Wow, marvelous weblog layout! How long have you ever been blogging for?
    you make blogging glance easy. The overall look of your web site is excellent, as neatly as the content!

  6. SamuelBix表示:

    where to get cheap clomid: buy generic clomid without prescription – order cheap clomid now

  7. Тут можно преобрести купить взломостойкие сейфы сейф взломостойкий

  8. RobertJar表示:

    buy prednisone online paypal: cheap prednisone online – prednisone 2 mg

  9. Excellent pieces. Keep writing such kind of information on your page.
    Im really impressed by your blog.
    Hey there, You have done a fantastic job. I’ll certainly digg
    it and individually suggest to my friends. I am confident they’ll be benefited from this web site.

  10. Thanks for sharing such a fastidious opinion, post is fastidious, thats why i
    have read it completely

  11. KennethPah表示:

    can i buy generic clomid without rx clomidonpharm cheap clomid for sale

  12. Nikefut表示:

    tor markets links dark web websites darknet links

  13. Appreciate the thorough analysis. For more, visit website design

  14. SamuelBix表示:

    amoxicillin 500 mg online: where to buy amoxicillin – amoxicillin capsule 500mg price

  15. KennethPah表示:

    can you buy amoxicillin uk amoxicillin online purchase amoxicillin 500 mg capsule

  16. Ꭲhhis isѕ tthe perfecht ƅoog forr aybody wwho rdalⅼy wantss tto finjd oout aboput this topic.
    Yoou undetstand soօ muᥙch itss amost touhh too
    arfue with yoou (not thyat I persxonally wull need to…HaHa).Youu certainly puut a brand nnеw
    spoin onn a toppic hat hass bbeen wrotten aout ffor ages.

    Great stuff, jjust excellent!

    Feell feee tto sսrff tto mmy weebpage :: ورود به بهترین سایت

  17. price gold表示:

    When someone writes an article he/she maintains the idea
    of a user in his/her mind that how a user can be aware of it.
    Therefore that’s why this piece of writing is great. Thanks!

  18. Hi there! I know this is kinda off topic but I was wondering which blog platform are
    you using for this website? I’m getting sick
    and tired of WordPress because I’ve had issues with hackers and I’m looking at alternatives for another
    platform. I would be fantastic if you could point me
    in the direction of a good platform. https://Palmer-Electrical.com/question/comptoir-de-cuisine-en-stratifie-un-choix-pratique-et-economique-2/

  19. KennethPah表示:

    get generic clomid without rx clomidonpharm where to buy generic clomid without a prescription

  20. gold price表示:

    Wow, this piece of writing is fastidious, my younger sister is analyzing these kinds of things,
    therefore I am going to let know her.

  21. KennethPah表示:

    buy amoxicillin where can i buy amoxocillin buy amoxicillin 500mg

  22. price gold表示:

    Thank you, I’ve recently been searching for
    information approximately this topic for a while and yours is the best I’ve discovered so far.

    However, what about the bottom line? Are you certain in regards to
    the source?

  23. Why Nobody Cares About Specializes In Asbestos Litigation Asbestos lawyers

  24. That is a great tip especially to those new to the blogosphere.
    Brief but very accurate information… Many thanks for sharing
    this one. A must read post!

發佈留言

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