How to Find Duplicate Elements in an Array - Java Program | Java Interview Question and Answer

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.พ. 2023
  • How to Find Duplicate Elements in an Array - Java Program | Java Interview Question and Answer | Test Automation Central
    For source code, visit - testautomationcentral.com/how...
    Automation Testing Interview Question | Selenium WebDriver Java Interview Question | Automation Tutorials | Selenium Tutorials | Testing Automation Tutorials | Test Automation | Java Selenium | Automation Interview Question And Answers | ChromeDriver Class | WebDriver Interface
    #sdet #seleniumtutorial #automationtester #automationtesting #javaprogramming #javatutorial #javaprogram #javaquestions #testautomation #testautomationcentral #seleniumtutorialforbeginners #seleniumwebdriver #javaselenium #seleniuminterview #finaljava
    #seleniumforbeginners #sdetinterview #seleniumquestions #chromedriver #webdriver #webdriverinterface #seleniumwebdriver #windowhandles #javaprogram #javaprogramming #javaarrays
  • แนวปฏิบัติและการใช้ชีวิต

ความคิดเห็น • 32

  • @TestAutomationCentral
    @TestAutomationCentral  ปีที่แล้ว +1

    For detailed tutorials and source code, visit - testautomationcentral.com/how-to-find-duplicate-elements-in-an-array-java-program-java-interview-question-and-answer/

  • @ViktorTheRook
    @ViktorTheRook 3 หลายเดือนก่อน

    U can sort array, iterate array and check array index i and i +1 until end of array… constant space and O(nlogn) because of sort…. Or use hash map for linear time but also linear space

  • @akaki_khotcholava
    @akaki_khotcholava 6 หลายเดือนก่อน +8

    Time complexity is O(n**2). You can easily use HasMap and one for loop. I can provide JS code. And time complexity for this code will be O(n)
    function findDuplicates(arr) {
    const elementCount = {};
    const duplicates = [];
    arr.forEach((element) => {
    // Initialize count for the element or increment existing count
    elementCount[element] = (elementCount[element] || 0) + 1;
    // Check if the element is a duplicate and hasn't been added to duplicates array yet
    if (elementCount[element] === 2) {
    duplicates.push(element);
    }
    });
    return duplicates;
    }

    • @JohnWickea
      @JohnWickea 3 หลายเดือนก่อน

      lol your code is worse . check your space complexity xD. I can solve all this in O(n) lol.

    • @akaki_khotcholava
      @akaki_khotcholava 3 หลายเดือนก่อน

      @@JohnWickea You must be a genius.

    • @ladro_magro5737
      @ladro_magro5737 2 หลายเดือนก่อน +1

      taking extra space is also not a good solution
      there is a much better way to handle this case with no extra space and O(n) time complexity

    • @JohnWickea
      @JohnWickea 2 หลายเดือนก่อน

      @@ladro_magro5737 let the kimd live in it's own universe, since he's not listening to anyone 😂

    • @ahmedshaltout4437
      @ahmedshaltout4437 13 วันที่ผ่านมา

      any operation on hashmap cost O(log N) time

  • @crosswalker45
    @crosswalker45 4 หลายเดือนก่อน

    Instead of brute force, try to implement other approach like hsshmap.

  • @josejoy174
    @josejoy174 10 หลายเดือนก่อน +1

    why not just use "for each " element?..

  • @cicartaya
    @cicartaya 10 หลายเดือนก่อน +2

    I'm just now learning about data structures and algorithms in C++. Correct me if I'm wrong, but I think this approach has a time complexity of O(n^2) because of the nested for loops, which is not that good if the arrays are really large. Is there a better/more efficient way to find duplicates in an array in Java?

    • @sergeiivanov5739
      @sergeiivanov5739 9 หลายเดือนก่อน +2

      Sets...

    • @electricity2703
      @electricity2703 6 หลายเดือนก่อน

      It takes combination(array.size(), 2) iteration

  • @mySofCh786
    @mySofCh786 5 หลายเดือนก่อน

    Don't use 2 for loop, don't use set/hashset

  • @arsalanalam9622
    @arsalanalam9622 2 หลายเดือนก่อน

    It doesn't work when you have multiple duplicate elements in same array

  • @rubenbarkhudaryan5606
    @rubenbarkhudaryan5606 10 หลายเดือนก่อน

    it doesnt work for 10000 elements arra

  • @jasonosunkoya
    @jasonosunkoya 2 หลายเดือนก่อน +1

    Just use a hashmap,

    • @nikhilraj1812
      @nikhilraj1812 8 วันที่ผ่านมา +1

      Hey bro can we use two pointer for this question?👀

  • @anubhav.codess
    @anubhav.codess 7 หลายเดือนก่อน +1

    Using hashmap and print the elements whose frequency is greater than 1 easy pezzy

    • @theanimemanofindia8036
      @theanimemanofindia8036 6 หลายเดือนก่อน

      Bro it is for the people who are only say core Java experience not collection

  • @rgbgaming11
    @rgbgaming11 10 หลายเดือนก่อน +1

    We can simply use stream for this

    • @prady1006
      @prady1006 10 หลายเดือนก่อน

      In interview they will tell u stream is doing fine. Create your logic then you have to learn basic bro

  • @Omkar_manjare
    @Omkar_manjare หลายเดือนก่อน

    Not optimized solution

  • @dekhlohhgftgdrf6038
    @dekhlohhgftgdrf6038 หลายเดือนก่อน

    What if all numbers were same let's say 1

  • @Thd369
    @Thd369 ปีที่แล้ว

    Good

  • @eccomercebeast8911
    @eccomercebeast8911 10 หลายเดือนก่อน +2

    or you can use list
    arr.stream().filter(x -> x == 1).toList();

    • @ChatGPT......
      @ChatGPT...... 8 หลายเดือนก่อน

      😢wah bhi

    • @akashsarjine
      @akashsarjine 4 หลายเดือนก่อน

      Use distinct

  • @nihanshupethe8983
    @nihanshupethe8983 10 หลายเดือนก่อน

    Bc 🔥🔥

  • @shaikafrid2186
    @shaikafrid2186 10 หลายเดือนก่อน

    It will fail for some edge cases

    • @subhojitbiswas3876
      @subhojitbiswas3876 9 หลายเดือนก่อน

      Ye s like if in the last if u have one more 1

  • @bikramprajapati5358
    @bikramprajapati5358 ปีที่แล้ว +1

    Data structure sikh pehle