How to Find Duplicates Elements in Java Array? - Java Interview Questions -5

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ย. 2024

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

  • @selectiveeagle4040
    @selectiveeagle4040 5 ปีที่แล้ว +5

    King of the explanation, very clear and up to point. God Bless you!

  • @ankurbansal904
    @ankurbansal904 6 ปีที่แล้ว +18

    String names[]={"Ankur","Hero","Arun","Ankur","Hero"};
    HashMap map = new HashMap();
    for (String ch : names) {
    if (map.containsKey(ch)) {
    int val = map.get(ch);
    map.put(ch, val + 1);
    } else {
    map.put(ch, 1);
    }
    }
    System.out.println(map);
    }
    }

    • @nazimkhan9080
      @nazimkhan9080 3 ปีที่แล้ว

      String names[]= {"Java","javaScript","Ruby","C","Python","Java","C"};
      Map storeMap=new HashMap();
      for(String name:names){
      Integer count= storeMap.put(name, 1);
      if(count!=null){
      System.out.println("Duplicate element is:::"+name);}}

  • @tawbinable
    @tawbinable 5 ปีที่แล้ว

    I had this question in 3 consecutive interviews recently. But the only difference is they asked me to find duplicate and non-duplicate characters from a given string such as "ascedcsf". Your video is really very well organized and well explained.

  • @kshitijshrivastava9881
    @kshitijshrivastava9881 6 ปีที่แล้ว +2

    There is one more question where we need to give the number of times a specific element is present i.e. for below array
    String nameArray[] = {"Java","C#","Ruby","Python","Java","C++","C#","AngularJS","Java"};
    it should return 3 for Java and 2 for C#.
    For this we just need to add one extra line in HaspMap code:
    System.out.println("The occurence of element "+entry.getKey()+" is "+ entry.getValue());
    Full code below:
    String nameArray[] = {"Java","C#","Ruby","Python","Java","C++","C#","AngularJS","Java"};
    Map map = new HashMap();
    for(String name: nameArray)
    {
    Integer count = map.get(name); // it will return null as no values are present for the key
    if(count==null)
    {
    map.put(name, 1); //when its null add the value
    }
    else
    {
    map.put(name, ++count); // when it finds second occurrence of Java (i.e count =1 )we increase the count
    }
    }
    System.out.println(map);
    for(Map.Entry entry:map.entrySet())
    {
    if(entry.getValue()>1)
    {
    System.out.println("The duplicate element is: "+entry.getKey());
    System.out.println("The occurence of element "+entry.getKey()+" is "+ entry.getValue());
    }

    }

  • @Tidda420
    @Tidda420 3 ปีที่แล้ว +3

    Your videos are really awesome, never saw anyone explaining things at this level. Please carry on for other concepts too.

  • @chayakakati1110
    @chayakakati1110 5 ปีที่แล้ว +4

    Hi Naveen, Thanks for the amazing videos. I am preparing for Selenium with Java interview and referring your videos and I must say the way you explain, the concepts becomes very easy to understand for a JAVA naive like me as well.
    One request, please increase the font size as its difficult to read your code in such small font. Thanks.....

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

    Your teaching is apple of my eye

  • @ayazattar1641
    @ayazattar1641 6 ปีที่แล้ว +5

    The third solution can be optimized
    If(hashmap.get(name) == null){
    Hashmap.put(name,counter)
    }
    else {
    Print duplicate element(name)
    }
    In this case we need not go for entryset

    • @averageconsumer5374
      @averageconsumer5374 5 ปีที่แล้ว

      not optimised. if you are going to print like that, duplicate elements will printed many times

  • @SmartProgramming
    @SmartProgramming 6 ปีที่แล้ว +2

    awesome tutorial sir, thanks a lot for sharing, keep it up 👍👍🙂🙂

  • @selvakumarthevar518
    @selvakumarthevar518 6 ปีที่แล้ว +2

    Hey Naveen, Another option is to use keySet() to get the getValue()

  • @bharathmatta3857
    @bharathmatta3857 6 ปีที่แล้ว +2

    Hi Naveen, your explanation of the concepts are too good. I have learnt so many things while going thru your videos. One suggestion I would like to let you know that please increase font so it will be more visible when we watch your trainings in mobile as well.
    Thanks,
    Bharath.

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

    you are the best; i appreciate the work and time,

  • @shubhamprajapati7150
    @shubhamprajapati7150 2 ปีที่แล้ว

    Aap kaafi achha padha rahe ho sir

  • @rohitsable9341
    @rohitsable9341 7 ปีที่แล้ว +2

    Very well explain naveen thank you. I understand each & every part because of this video. I need optimize solution for few assignment like longest substring of non repeating character, intersecting of rectangles, no repeating first character in array, expression validation in java. If you give any idea it would help me lot .

  • @antarabanerjee7314
    @antarabanerjee7314 3 ปีที่แล้ว

    Thanks Naveen, have been struggling a lot to get this understanding, thanks a ton! 😊😊

  • @gangadharmishra2138
    @gangadharmishra2138 6 ปีที่แล้ว

    Thanks Naveen. I understand each and every part because of this video.

  • @kalidindiprashanth7363
    @kalidindiprashanth7363 5 ปีที่แล้ว +6

    If i have 5 billions elements in java array along with duplicates then also is this the best solution.Because i have been asked this question in interview but i couldn't give them answer.

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

    String[] names = {"java","phython","java","c#","phython",".net"};
    Arrays.stream(names)
    .filter(name -> Collections.frequency(Arrays.asList(names), name) > 1)
    .collect(Collectors.toSet())
    .forEach(System.out::println);

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

    Hi Naveen , If we have n same element , it will print n-1 times the same element for the set solution you provided.For example {harsh,raj, harsh,kumar,harsh} . output will be harsh, harsh

  • @maksymosipa9287
    @maksymosipa9287 5 ปีที่แล้ว +3

    100% they will ask you, in each video on selenium and java you are telling this ))

    • @dfeefwfg141
      @dfeefwfg141 5 ปีที่แล้ว

      yea because whatever he is teaching everything asked in interview atleast i got several interview questions from his videos

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

    Nice video concept is so clear

  • @cookhjj
    @cookhjj 5 ปีที่แล้ว

    We can iterate hash map like this
    for(Map.Entry entrySet : map.entrySet())
    {
    if((int)entrySet.getValue()>1)
    System.out.println("Duplicate element in an array:"+ (String)entrySet.getKey());
    }

  • @reetu1326
    @reetu1326 5 ปีที่แล้ว

    One of the best explanation I found.Thanks a lot, for sharing.

    • @vanajabethina5991
      @vanajabethina5991 5 ปีที่แล้ว

      w.a p to find the words number of time repeating using hashmap (string="web internet web chrome internet safari") how can i solve please tell me frds or any video links share me frds last intreviewer
      ask me

  • @MultiPrasad99
    @MultiPrasad99 5 ปีที่แล้ว +3

    Using HashMap also we can make it simple. I am checking for duplicate numbers so code is written with int. Like this...
    int a[]= {2,2,4,6,7,6,9,9};
    HashMap map = new HashMap();
    for(Integer num1:a) {
    if(map.put(num1, num1)!=null) {
    System.out.println("Duplicate using Hashmap is: "+ num1);
    }
    }

  • @sanjuk2484
    @sanjuk2484 5 ปีที่แล้ว

    Your genius naveen

  • @GulbargaRamNavamiUstav
    @GulbargaRamNavamiUstav 5 ปีที่แล้ว +2

    I seen many channels but not standard one like this

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

    Thank you so much Naveen.. Really helpful..

  • @safarnama6608
    @safarnama6608 5 ปีที่แล้ว

    one of the best solutions i have seen, it was so easy to understand the concepts. Thanks Naveen Sir:)

  • @AjayKumar-ye2cu
    @AjayKumar-ye2cu 5 ปีที่แล้ว

    Best solution is HashMap because 1st(Bruteforce) and 2nd(Set) can only compare 2 elements.But using HashMap we can find N no. of duplicate elements

    • @Nancy-xd7bp
      @Nancy-xd7bp 5 ปีที่แล้ว

      can you provide the solution with HashMap?

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

      @@Nancy-xd7bp here you go:String a1[]= {"java","php","python","java","c","c#","php","java"};
      HashMap map=new HashMap();
      for(String myNames : a1)
      {
      if(map.containsKey(myNames))
      {
      int count=map.get(myNames);
      map.put(myNames, count+1);
      }
      else
      {
      map.put(myNames, 1);
      }
      }
      System.out.println(map);

    • @kiranemmila
      @kiranemmila 4 ปีที่แล้ว

      @@priteshpatel4312 superb solution...thank you

  • @shoryabeohar1816
    @shoryabeohar1816 7 ปีที่แล้ว +8

    thanks amzing videao ..one request could you pls increase the fonts of eclipse..would be a great while watching

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

    Thanks Naveen. That was crystal clear!

  • @anuradharamisetty
    @anuradharamisetty 6 ปีที่แล้ว

    Really good teaching

  • @savitha1110
    @savitha1110 6 ปีที่แล้ว

    By using Hashset ---> This will print Java 2 times , if the there more than 2 occurrence of the same element . {"Java","Java","Java"}

  • @saisrikar7987
    @saisrikar7987 6 ปีที่แล้ว

    excellent video sir.Please make more videos regd Java

  • @kirtihoney3822
    @kirtihoney3822 5 ปีที่แล้ว

    super explaination please zoom the code while you explaining for better view..sometimes its not clear view

    • @BikramTechie
      @BikramTechie 3 ปีที่แล้ว

      The code is clearly visible, you can change . you can change quality to 720p or 1080p

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

    for String s = {"Java", "JavaScript", "Ruby", "Python", "Java", "C", "C", "Java"}, The first solution will not work. It will show "Java" Three times. Anyways, I would like to thank you for making these great tutorials.

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

    Naveen Thanks for sharing video but your logic using Hashset will not work if we will have more than two elements having same vaues in an array. Can You check that case?

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

    Thanks naveen, also could you please make same thing using recursive function. It will help us.

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

    Amazing broad concepts !

  • @ghulamanosh5564
    @ghulamanosh5564 4 ปีที่แล้ว

    Thank you Naveen.

  • @vinodaraaj
    @vinodaraaj 2 ปีที่แล้ว

    Using HashMap also we can make it simple. but when we need to get the total count of duplicate we can modify the value fi key exist .

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

    Hey Naveen u can do video for How to Find Duplicates Elements in Normal Int arrays?

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

    I like Hashset :)

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

    sir, please help me i really confuse between them what is the difference between the "array.length and array.length()" some time array.length will work and some time array.length() will work. is there is any rules for is for using it? with String arr.length() will work but when we use an integer type, its work with array.length.

  • @apzalbahin
    @apzalbahin 3 ปีที่แล้ว

    String[] dupArray={"Mumbai","Chennai","Delhi","Kochi","Chennai","Kochi","Trivandrum","Delhi"};
    Map myMap=new HashMap();
    int counter=0;
    for (String city:dupArray) {
    if(myMap.put(city,++counter)!=null){
    System.out.println("Duplicate Value="+city);
    }
    }

  • @ayeshazakria2215
    @ayeshazakria2215 3 ปีที่แล้ว

    Do you have series of Data Structures ?

  • @ranjanentertainment5602
    @ranjanentertainment5602 7 ปีที่แล้ว +2

    Hey Naveen,
    thanks for uploading such type of program but i have one inside (store.addname==false)
    Set s=new HashSet();
    for (String name : names) {
    if(store.add(name)==false){

  • @sheekhachauhan
    @sheekhachauhan 4 ปีที่แล้ว

    write a javascript code to perform the following on n array N containing the following{"welcome", "friends"} (i)Add "try" to array N.(ii)Join array N to another array M. (iii)Delete the last element from the array M. (iv)sort the array (v)Display the array in reverse. pls solve this.. if u're a real teacher

  • @jarvisfriday7452
    @jarvisfriday7452 4 ปีที่แล้ว

    Hi Naveen, Can we use Treeset instead of hashset?

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

    please add printing pyramid pattern in java interview question

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

    in first example (int i=0 ; i

  • @suhaschavan8406
    @suhaschavan8406 4 ปีที่แล้ว

    Navin, need help can you suggest solution for finding int duplicate from scanner

  • @ssandeep79
    @ssandeep79 6 ปีที่แล้ว

    What if we sort the array and then compare only contiguous cells /indeces? That might be quicker.

  • @sanjayadahal1743
    @sanjayadahal1743 4 ปีที่แล้ว

    Hi Navee,n for HashMap in the else statement if i print the item or simple return that item do i really need to iterate over hashMap to find the count having value greater than 1.

  • @gadinaresh5458
    @gadinaresh5458 5 ปีที่แล้ว

    In those concepts which is efficient way to use.....so plzz reply

  • @mandeep3445
    @mandeep3445 4 ปีที่แล้ว

    Thank you.

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

    Hi Naveen,
    thanks for sharing.iam getting output as shown below, how can i get only one value, can u solve please
    String names[]= {"java","phython","java","c#","phython",".net"};
    Map storemap=new HashMap();
    for(String name : names) {
    Integer count=storemap.get(name);
    if(count==null) {
    storemap.put(name, 1);
    }else {
    storemap.put(name,++count);
    }
    //get the value from this hashmap
    Set entryset= storemap.entrySet();
    for(Entry entry : entryset) {
    if (entry.getValue()>1) {
    System.out.println("Duplicate value is :"+entry.getKey());
    }
    }
    }
    output:
    Duplicate value is :java
    Duplicate value is :java
    Duplicate value is :java
    Duplicate value is :phython
    Duplicate value is :java
    Duplicate value is :phython

    • @varshab9967
      @varshab9967 6 ปีที่แล้ว

      Hi Srividya,
      End the first for loop before getting the value from HashMap.Hope this will help

    • @vishnuvardhanreddya2663
      @vishnuvardhanreddya2663 5 ปีที่แล้ว

      String names[]= {"java","phython","java","c#","phython",".net"};

      Map storemap=new HashMap();
      for(String name : names) {
      Integer count=storemap.get(name);
      if(count==null) {
      storemap.put(name, 1);
      }else {
      storemap.put(name,++count);
      }
      //get the value from this hashmap

      }
      Set entryset= storemap.entrySet();
      for(Entry entry : entryset) {
      if (entry.getValue()>1) {
      System.out.println(entry.getValue()+" times string "+entry.getKey()+"duplicated");
      }

      }

  • @ayeshazakria2215
    @ayeshazakria2215 3 ปีที่แล้ว

    pls use simple for loops

  • @fred2647
    @fred2647 3 ปีที่แล้ว

    Hey everyone, can anyone help me to find answer to this question? When we make use of in-built Java instructions in the video to check if the element is already in a Set, or HashMap, .... (e.g. , if (map.containsKey(ch))......) does Java do an additional iteration internally (abstract to the programmer) to check the whole set or hash map for the duplicates and therefore adding to the time complexity of the whole code?

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

    I think there is the bug in this code I am not sure , but let assume String a[] = {"Java", "Python", "C", "C#", "C++", "Go", "C", "C", "Java", "Java"}; how this should with your code .

  • @ravirsoni
    @ravirsoni 6 ปีที่แล้ว

    Why here Object created with Parent Interface Reference ?
    We can also create the object as per below whats the problem here ?
    HashMap storeMap = new HashMap() instead of Map storeMap = new HashMap();
    HashSet store = new HashSet() instead of Set store = new HashSet();
    can someone explain this please ? Thanks in Advance.

  • @anilpal8333
    @anilpal8333 5 ปีที่แล้ว

    Hi how internally set remove duplicate in o(n) can you explain it
    Which add method call and how it will remove any object which is duplicate.

    • @sayanbhaumik5739
      @sayanbhaumik5739 5 ปีที่แล้ว

      Set by default stores only unique value in it. So here add(value) checks if the particular value is stored in it or not. If it finds the value then it will not add it in the set otherwise it will add. Let me know if i am clear.

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

      @@sayanbhaumik5739 how it will verify weather it object is present previously if bean object doesn't override hashcode and equals method

  • @Randoms147
    @Randoms147 5 ปีที่แล้ว

    which ide u used in this video?

  • @relax_habibi
    @relax_habibi 2 ปีที่แล้ว

    Hi naveen, i could not find a solution to check for duplicates in this array: integer array [12,12,7,7,7,7,7] , can you please help?

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

      public static void main(String[] args) {
      int[] arr = {1, 5, 12, 12,7,7,7,7};
      Set uniqueSet = new HashSet();
      Set duplicates = new HashSet();
      for (int num : arr) {
      if (!uniqueSet.add(num)) {
      duplicates.add(num);
      }
      }
      if (!duplicates.isEmpty()) {
      System.out.println("Duplicate elements in the array without repetition:");
      for (int duplicate : duplicates) {
      System.out.println(duplicate);
      }
      } else {
      System.out.println("There are no duplicate elements in the array");
      }
      }

  • @piyumigunawardana4610
    @piyumigunawardana4610 5 ปีที่แล้ว

    thank you!!

  • @bishalram481
    @bishalram481 6 ปีที่แล้ว

    Can you explain why the j value is not starting from 0 why it's started j=I+1;

    • @sachin21890
      @sachin21890 6 ปีที่แล้ว

      you would not want to compare any element with itself, otherwise all the elements will be printed as duplicate. Its j=i+1 because you want to compare with next element onward.

  • @sarathkumar4845
    @sarathkumar4845 2 ปีที่แล้ว

    How to find non duplicates in java please help me

  • @abhinashpatro2922
    @abhinashpatro2922 5 ปีที่แล้ว

    what is time complexity?

    • @fcgisarah
      @fcgisarah 5 ปีที่แล้ว

      The amount of time it takes for completion.

  • @nazimkhan9080
    @nazimkhan9080 3 ปีที่แล้ว

    String names[]= {"Java","javaScript","Ruby","C","Python","Java","C"};
    Map storeMap=new HashMap();
    for(String name:names){
    Integer count= storeMap.put(name, 1);
    if(count!=null){
    System.out.println("Duplicate element is:::"+name);}}