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,411,934 Responses

  1. bit.ly表示:

    What’s up mates, its wonderful paragraph regarding educationand fully explained, keep it up all the time.

  2. 강남호빠表示:

    You really make it seem so easy with your presentation but I find this topic to be actually something that I think I would never understand.
    It seems too complex and extremely broad for
    me. I’m looking forward for your next post, I’ll try to get the hang of it!

  3. seo price uk表示:

    %%

    Review my website :: seo price uk

  4. Whаt’s սp to еvery one, fⲟr tһe reason thnat I am ɑctually keen of reading tһis webpage’ѕ
    post to be updated regularly. It incⅼudes fastidious stuff.

    Heree іs my site :: cryptometrics101

  5. Realldoll表示:

    %%

    Also visit my website: Realldoll

  6. 花蓮包車表示:

    you are in point of fact a excellent webmaster.
    The site loading pace is amazing. It sort of feels that you are doing any distinctive trick.
    Moreover, The contents are masterpiece. you have done a magnificent job on this subject!

  7. AMBBET表示:

    Truly when someone doesn’t know after that its up
    to other users that they will help, so here it happens.

  8. utube表示:

    Someone essentially lend a hand to make critically articles I’d state.

    This is the very first time I frequented your web page and so far?

    I surprised with the analysis you made to create this particular publish amazing.
    Great task!

  9. Hey would you mind letting me know which webhost you’re working with?
    I’ve loaded your blog in 3 different internet browsers and
    I must say this blog loads a lot faster then most.

    Can you suggest a good internet hosting provider at a honest price?

    Many thanks, I appreciate it!

  10. seo prices uk表示:

    %%

    Also visit my site seo prices uk

  11. buy Love doll表示:

    %%

    Feel free to visit my page; buy Love doll

  12. cheats表示:

    Hi there, yup this piece of writing is genuinely nice and I have learned lot of things
    from it on the topic of blogging. thanks.

  13. Alana表示:

    Ӏ don’t even қnow the way I ended up here, howevеr Ι believed this post was good.
    I don’t understand ԝwho you’re but definitеoy you are going to a well-known blogger in the еvent you are not already.

    Cheers!

  14. Buy Sex dolls表示:

    %%

    Also visit my web page :: Buy Sex dolls

  15. When someone writes an paragraph he/she keeps the idea of a user in his/her
    brain that how a user can know it. Therefore that’s why this piece of writing is perfect.
    Thanks!

  16. I’ve been exploring for a little bit for any high quality articles or weblog posts on this kind of area .
    Exploring in Yahoo I at last stumbled upon this website. Reading this information So i am satisfied to show that I’ve an incredibly
    just right uncanny feeling I came upon exactly what I needed.
    I most no doubt will make certain to do not put out of your mind this site and provides
    it a look regularly.

  17. %%

    Feel free to visit my web page; customizing Sexdoll

  18. Se x dolls表示:

    %%

    my website … Se x dolls

  19. Togel表示:

    %%

    Also visit my web-site: Togel

  20. база для хрумера купить

    Вы занимаетесь продвижением сайтов?
    Тогда вам к нам. [url=https://goo.su/4zRx]Наша группа в телеграмме[/url].У нас вы можете выбрать
    любую нужную для вашей работы базу для прогонов Xrumer и GSA, allsubmitter по
    разной ценовой категории.успешные базы для хрумера

發佈留言

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