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

Sdílet
Vložit
  • čas přidán 19. 02. 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
  • Jak na to + styl

Komentáře • 34

  • @TestAutomationCentral
    @TestAutomationCentral  Před rokem +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/

  • @akaki_khotcholava
    @akaki_khotcholava Před 7 měsíci +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 Před 4 měsíci

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

    • @akaki_khotcholava
      @akaki_khotcholava Před 4 měsíci

      @@JohnWickea You must be a genius.

    • @ladro_magro5737
      @ladro_magro5737 Před 3 měsíci +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 Před 3 měsíci

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

    • @ahmedshaltout4437
      @ahmedshaltout4437 Před měsícem +1

      any operation on hashmap cost O(log N) time

  • @ViktorTheRook
    @ViktorTheRook Před 4 měsíci

    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

  • @cicartaya
    @cicartaya Před 11 měsíci +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?

  • @arsalanalam9622
    @arsalanalam9622 Před 3 měsíci +1

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

  • @vedantjoshi2093
    @vedantjoshi2093 Před 26 dny

    No need for two for loops..it can be done in one

  • @anubhav.codess
    @anubhav.codess Před 8 měsíci +1

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

    • @theanimemanofindia8036
      @theanimemanofindia8036 Před 7 měsíci

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

  • @Pearl0812
    @Pearl0812 Před 10 dny

    How to remove that??

  • @crosswalker45
    @crosswalker45 Před 5 měsíci

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

  • @mySofCh786
    @mySofCh786 Před 6 měsíci

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

  • @rgbgaming11
    @rgbgaming11 Před 11 měsíci +1

    We can simply use stream for this

    • @prady1006
      @prady1006 Před 11 měsíci

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

  • @jasonosunkoya
    @jasonosunkoya Před 3 měsíci +1

    Just use a hashmap,

    • @nikhilraj1812
      @nikhilraj1812 Před měsícem +1

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

  • @josejoy174
    @josejoy174 Před 11 měsíci +1

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

  • @eccomercebeast8911
    @eccomercebeast8911 Před 11 měsíci +2

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

  • @rubenbarkhudaryan5606
    @rubenbarkhudaryan5606 Před 11 měsíci

    it doesnt work for 10000 elements arra

  • @Thd369
    @Thd369 Před rokem

    Good

  • @dekhlohhgftgdrf6038
    @dekhlohhgftgdrf6038 Před 2 měsíci

    What if all numbers were same let's say 1

  • @Omkar_manjare
    @Omkar_manjare Před 2 měsíci

    Not optimized solution

  • @nihanshupethe8983
    @nihanshupethe8983 Před 11 měsíci

    Bc 🔥🔥

  • @shaikafrid2186
    @shaikafrid2186 Před 11 měsíci

    It will fail for some edge cases

  • @bikramprajapati5358
    @bikramprajapati5358 Před rokem +1

    Data structure sikh pehle