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

  1. 5 Killer Quora Answers On Shopping Online Uk Clothes Shopping online uk clothes

  2. Five Futon For Sale Near Me Projects For Any Budget Sofas on sale uk
    (Sustainabilipedia.Org)

  3. Fhoy.Kr表示:

    See What Boat Accident Lawsuit Tricks The Celebs Are
    Utilizing Boat Accident Lawsuit (Fhoy.Kr)

  4. Introduction To The Intermediate Guide For Hades Gods Tier List http://www.oscarreys.top

  5. If some one needs expert view concerning running a blog then i propose him/her to pay a visit this
    web site, Keep up the pleasant job.

  6. Five Killer Quora Answers To Best Online Shopping Websites Uk Best Online shopping Websites uk

  7. 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. https://wolvesbaneuo.com/wiki/index.php/Comment_Trouver_La_Bonne_Paire_De_Lunettes_Par_Votre_Mode_Et_Votre_Vision

  8. Over the course of the last century, asbestos settlement was used in a variety of products to make
    them more durable, less prone to fire and less expensive.
    But exposure to airborne asbestos fibers can lead to lung diseases such as mesothelioma and asbestosis.

  9. See What Uk Online Shopping Sites For Mobile Tricks The Celebs Are Using Uk online shopping Sites for Mobile

  10. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://mobilitatelx.es/tjzno of type and scrambled it to make a type specimen book.

  11. I do not know if it’s just me or if perhaps everyone else
    encountering problems with your website.
    It looks like some of the written text on your posts are running off the screen. Can somebody else please provide
    feedback and let me know if this is happening to them too?
    This may be a issue with my internet browser because I’ve had this happen previously.
    Appreciate it

  12. We stumbled over here by a different website and thought
    I might as well check things out. I like what I see so now i am following you.

    Look forward to checking out your web page again.

  13. dob表示:

    Приветствую. Может кто знает, где почитать полезные блоги о недвижимости? Сейчас читаю https://an72.ru

  14. dob表示:

    Всем привет! Подскажите, где почитать разные статьи о недвижимости? Сейчас читаю https://an72.ru

  15. cassylawn表示:

    How To Make An Amazing Instagram Video About Mesothelioma Asbestos Lung Cancer cassylawn

  16. 5 Killer Quora Answers On Window Doctor Near Me window doctor Near me

  17. Pills information sheet. Drug Class.
    get cheap inderal for sale
    Actual news about drug. Get here.

  18. If some one needs to be updated with latest technologies then he must be pay a visit this web site and be up to
    date all the time.

  19. 25 Shocking Facts About Online Shopping Clothes Uk Cheap Micro Terror Sound Quality

  20. Why Is Everyone Talking About Upvc Windows Right Now upvc Windows repairs near me

發佈留言

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