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,246,045 Responses

  1. DarrenUnjuh表示:

    https://iverfast.com/# stromectol price

  2. strategic表示:

    Do you mind if I quote a few of your posts as long as I provide credit and sources back
    to your website? My blog is in the exact same area of interest as
    yours and my visitors would really benefit from a lot of the information you provide here.

    Please let me know if this okay with you. Many thanks!

  3. Jav porn表示:

    Great post. I was checking constantly this blog
    and I am impressed! Extremely useful info specifically the last part 🙂 I care for such
    info much. I was seeking this certain info for a very
    long time. Thank you and good luck.

  4. Sazreal表示:

    Всё, что нужно знать о покупке аттестата о среднем образовании
    moneysweet.listbb.ru/viewtopic.php?f=2&t=4955

  5. На нашем сайте вы сможете купить дженерики в СПБ с доставкой курьером в день заказа подробности по ссылке
    https://men78.ru гарантированное качество от производителя на все препараты для повышения потенции

  6. 10 Meetups About Fabric Corner Recliner Sofa You Should Attend Navy Fabric Corner Sofa

  7. Sazrmyw表示:

    Покупка диплома о среднем полном образовании: как избежать мошенничества?

    cyberpinoy.net/create-blog/

  8. I really like your blog.. very nice colors &
    theme. Did you create this website yourself or
    did you hire someone to do it for you? Plz respond as I’m looking to create my own blog and would like to know where u got this from.
    cheers

  9. Awesome information Kudos!

  10. BrandenSlomy表示:

    cost generic clomid pill: cheap clomid – can you get generic clomid

  11. AlbertDuM表示:

    lisinopril 20mg: Lisinopril 1st – lisinopril1st

  12. These are actually enormous ideas in regarding blogging.
    You have touched some good factors here. Any way keep up wrinting.

  13. 123 go proxy表示:

    Hi there! I’m at work browsing your blog from my new iphone 3gs!
    Just wanted to say I love reading through your blog and look forward to all your posts!
    Keep up the superb work!

  14. Heya! I’m at work surfing around your blog from my new apple iphone!
    Just wanted to say I love reading through your blog and look forward to all your posts!

    Carry on the outstanding work!

  15. DarrenUnjuh表示:

    https://lisinopril1st.com/# lisinopril1st

  16. The cultural experiences in Dubai are unforgettable! Thanks to dubai hatta tour for the amazing recommendations

  17. Blakesmema表示:

    кухни на заказ цены — Узнайте стоимость изготовления кухонь на заказ.

  18. Great post. I was checking constantly this blog and I’m impressed!
    Very helpful information specifically the last part 🙂 I care for such
    info a lot. I was looking for this particular information for a long
    time. Thank you and good luck.

  19. You’ll Never Be Able To Figure Out This Adult Toys For Couple’s Tricks adult toys for couple (Yonghyeonip.com)

  20. Everything composed was very reasonable. However, think on this, what if you were to create a killer headline?
    I mean, I don’t want to tell you how to run your website, but what if you added a
    title to possibly get a person’s attention? I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室 is a little boring.
    You could glance at Yahoo’s home page and watch how
    they create post headlines to get viewers interested.
    You might add a related video or a related pic or two to grab people interested about everything’ve
    got to say. Just my opinion, it might bring your posts a little bit more interesting.

  21. Sazrhrz表示:

    Купить диплом старого образца, можно ли это сделать по быстрой схеме?

    maxima.2ua.in.ua/viewtopic.php?f=3&t=6453

發佈留言

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