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,449,322 Responses

  1. 20 Resources That Will Make You More Successful At Buy Taxi Driving License Online Without Exam
    internationellt köRkort – Transportstyrelsen

  2. выведение из запоя в нижнем новгороде выведение из запоя в нижнем новгороде .

  3. I like what you guys are up too. Such clever work and exposure!
    Keep up the wonderful works guys I’ve incorporated you
    guys to my own blogroll.

  4. Dannyfut表示:

    deep web search deep web links https://github.com/darknetmarkets24/darknet-markets – darknet drug store

  5. Unbelievably user friendly site. Great info available on few gos to.

    https://www.kopalniapracy.pl

  6. An additional crucial facet of efficiency training in esports is the psychological side of the game.

  7. See What How To Get An ADHD Diagnosis UK Tricks The Celebs Are
    Using how to get an adhd diagnosis uk

  8. CVFBGNHJMK表示:

    I am not sure where you are getting your information, but good topic.
    I needs to spend some time learning more or understanding
    more. Thanks for wonderful information I was looking for this information for my mission.

  9. The material is very exciting.
    nizanskie.info

  10. A fascinating discussion is worth comment. I think that you
    ought to publish more about this issue, it may not be a taboo
    matter but generally people don’t speak about these topics.
    To the next! Cheers!!

    Also visit my web page – thankyou.Eoapps.Co.kr

  11. uk money表示:

    These are actually wonderful ideas in about blogging.
    You have touched some fastidious things here. Any way keep up wrinting.

  12. You should be a part of a contest for one of the best blogs on the web.

    I most certainly will recommend this blog!

  13. Hello to all, how is everything, I think every one is getting more from this web page, and your views are good
    in support of new visitors.

  14. I blog frequently and I seriously appreciate your content.
    This great article has truly peaked my interest. I am
    going to bookmark your blog and keep checking for new details about once per
    week. I subscribed to your Feed too.

  15. вывод из запоя в стационаре нижнего новгорода vyvod-iz-zapoya-v-stacionare21.ru .

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

  17. I don’t even know how I ended up here, but I thought this
    post was great. I do not know who you are but certainly you are
    going to a famous blogger if you aren’t already 😉 Cheers!

  18. prop money表示:

    Hi there, just became alert to your blog through Google, and
    found that it is truly informative. I am going to watch out for
    brussels. I’ll appreciate if you continue this in future.

    A lot of people will be benefited from your writing.
    Cheers!

  19. Dannyfut表示:

    darknet marketplace dark web access https://github.com/darknetmarkets24/darknet-markets – darknet markets

  20. www.1Moli.top表示:

    See What Buy UK Driving License Tricks The Celebs Are Utilizing buy uk Driving license (http://www.1Moli.top)

發佈留言

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