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.
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()); }
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.....
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
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.
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 .
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.
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
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()); }
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
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); } }
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.
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?
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.
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){
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
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.
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
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"); }
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?
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 .
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.
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.
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"); } }
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.
King of the explanation, very clear and up to point. God Bless you!
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);
}
}
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);}}
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.
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());
}
}
Your videos are really awesome, never saw anyone explaining things at this level. Please carry on for other concepts too.
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.....
Your teaching is apple of my eye
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
not optimised. if you are going to print like that, duplicate elements will printed many times
awesome tutorial sir, thanks a lot for sharing, keep it up 👍👍🙂🙂
Hey Naveen, Another option is to use keySet() to get the getValue()
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.
you are the best; i appreciate the work and time,
Aap kaafi achha padha rahe ho sir
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 .
Thanks Naveen, have been struggling a lot to get this understanding, thanks a ton! 😊😊
Thanks Naveen. I understand each and every part because of this video.
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.
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);
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
100% they will ask you, in each video on selenium and java you are telling this ))
yea because whatever he is teaching everything asked in interview atleast i got several interview questions from his videos
Nice video concept is so clear
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());
}
One of the best explanation I found.Thanks a lot, for sharing.
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
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);
}
}
thanks brother!
Your genius naveen
I seen many channels but not standard one like this
Thanks for watching
Tqs for the reply sir
Thank you so much Naveen.. Really helpful..
one of the best solutions i have seen, it was so easy to understand the concepts. Thanks Naveen Sir:)
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
can you provide the solution with HashMap?
@@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);
@@priteshpatel4312 superb solution...thank you
thanks amzing videao ..one request could you pls increase the fonts of eclipse..would be a great while watching
Go to Google
Thanks Naveen. That was crystal clear!
Really good teaching
By using Hashset ---> This will print Java 2 times , if the there more than 2 occurrence of the same element . {"Java","Java","Java"}
excellent video sir.Please make more videos regd Java
super explaination please zoom the code while you explaining for better view..sometimes its not clear view
The code is clearly visible, you can change . you can change quality to 720p or 1080p
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.
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?
Thanks naveen, also could you please make same thing using recursive function. It will help us.
Amazing broad concepts !
Thank you Naveen.
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 .
Hey Naveen u can do video for How to Find Duplicates Elements in Normal Int arrays?
I like Hashset :)
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.
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);
}
}
Do you have series of Data Structures ?
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){
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
Hi Naveen, Can we use Treeset instead of hashset?
please add printing pyramid pattern in java interview question
in first example (int i=0 ; i
Navin, need help can you suggest solution for finding int duplicate from scanner
What if we sort the array and then compare only contiguous cells /indeces? That might be quicker.
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.
In those concepts which is efficient way to use.....so plzz reply
Thank you.
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
Hi Srividya,
End the first for loop before getting the value from HashMap.Hope this will help
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");
}
}
pls use simple for loops
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?
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 .
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.
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.
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.
@@sayanbhaumik5739 how it will verify weather it object is present previously if bean object doesn't override hashcode and equals method
which ide u used in this video?
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?
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");
}
}
thank you!!
Can you explain why the j value is not starting from 0 why it's started j=I+1;
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.
How to find non duplicates in java please help me
what is time complexity?
The amount of time it takes for completion.
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);}}