In the first for loop i should be less than arr.length()-1, bcz when i is at index 4, then j will be i+1 (i.e) 5 which will lead to ArrayIndexOutofBound exception
The given array is {"java","C","C++","python","java"} the first iteration java is compared with c c++ python and java but with iterations as it goes forward the second iteration c is compared with c++ python and java the third iteration c++ is compared with python and java the fourth iteration python is compared with java
I wrote this program . Bt when I take two duplicate(java,c) in the set . The out is showing like duplicate element is : java and next line duplicate element is c.
you're explained clearly and easily I understood how to find duplicate elements thank you so much, Sir. But the count of duplicate elements did not get the correct value when using the HashSet approach and I've given my coding below public class DuplicatesArrays { public static void main(String[] args) {
Boolean flag = false; Boolean dntdup = false; int count = 0; int count1 = 0; String [] ar = {"l","m","v","m","n","y","m"};
System.out.println(ar.length);
// using for loop for (int i = 0; i < ar.length; i++) { for (int j = i+1; j < ar.length; j++) { if (ar[i]==ar[j]) { count = count+1; System.out.println("found duplicate :" + ar[i]); flag = false;
} } }
System.out.println("count duplicate elements by for loop: "+count);
if (flag == true) { System.out.println("not found duplicate:");
}
//using hashset
HashSet h = new HashSet(); for (String l : ar) { if (h.add(l)==false) { count1 = count1+1; System.out.println("found duplicate :" + l);
} }
System.out.println("count duplicate elements by hashset :" + count1);
if (flag == true) { System.out.println("not found duplicate:");
@@kanithisatishkumar7036 Iterator i=langs.iterator(); while(i.hasNext()) { System.out.println(i.next()); } Use Iterator to print all the values in Hashset.
Can some body explain; on first logic first element is compared with elements after that, so say 3 rd element is compared with 4th element, it is not compared with first or second, Is that logic right???
Not needed because i=0, j=1 (arr[0]== arr[1]) comparison is already done to check for duplicates. So, there is no need of comparing again with the former i.e; i=1, j=0 arr[1] == arr[0]
Take the upper for loop once and check what it is printing ok Then check what the lower for loop checking Now put i = let's say 4 And then u put 2nd for loop you will see it will give values upto 4 ok so it always checks values hoto that point
the methods are time complexity are really bad. the 1st one is O(N^2). you can do it in O(N) with 1 loop and check for 3 or more duplicates/ the code:(same for String) class FindDuplicate { // Function to print duplicates void printRepeating(int arr[], int size) { int i; System.out.println("The repeating elements are : ");
for (i = 0; i < size; i++) { int j = Math.abs(arr[i]); if (arr[j] >= 0) arr[j] = -arr[j]; else System.out.print(j + " "); } }
// Driver code public static void main(String[] args) { FindDuplicate duplicate = new FindDuplicate(); int arr[] = { 1, 2, 3, 1, 3, 6, 6 }; int arr_size = arr.length;
duplicate.printRepeating(arr, arr_size); } } for String: String[] users = "User1,User2,User1,User,User".split(","); Set uniquUsers = new HashSet(); for (int i = 0; i < users.length; i++) { if (!uniquUsers.add(users[i])) users[i] = "Duplicate"; // here I am assigning Duplicate instead if find duplicate // you can assign as null or whatever you want to do with duplicates. } System.out.println(Arrays.toString(users));
suppose we have multiple duplicates like below and want to the print the output only once like duplicate elements = java,c++,python. Could you please guide me. String arr[] = {"java","python","c++","java","c","c++","devops","java","c++","python","python"};
Finally i found this in a very perfect way of explanation. Thank you ❤️
Welcome
In the first for loop i should be less than arr.length()-1, bcz when i is at index 4, then j will be i+1 (i.e) 5 which will lead to ArrayIndexOutofBound exception
Hahaha...asking or telling
It is not nessesary to compare the last element with others because it is already compared by other elements
Hash set way is life saver sir 👍
My pleasure
2 mistakes:
1. i loop should be from 0 to array length-1.
2. if condition inside j loop is incorrect.
for(int i=0; i
i
@@bhagathgudivaka1520 Yes because if i goes to 5, then j becomes 6 and it will return array index out of bounds error.
@@samK12_18 if j becomes 6 then inner loop will be stopped and Comes to outer loop again
first java is small j , last 2 java is capital J; how you prove multiple entry give same result?
great sir. plz upload latest trending logical programs java in automation interview.
we can directly write as if(flag) , it would look good right , though it is fine , thanks
The given array is {"java","C","C++","python","java"}
the first iteration java is compared with c c++ python and java but with iterations as it goes forward
the second iteration c is compared with c++ python and java
the third iteration c++ is compared with python and java
the fourth iteration python is compared with java
yes
it works as you said bcoz in first iteration "java" is already compared with all the elements
Ur the best Sir. thanku
Welcome
?? how can fix this if i have three same element ??
arr[]={"java","c","c++","python","java","java"};
output : java
java
java
Same error
Same error
I wrote this program . Bt when I take two duplicate(java,c) in the set . The out is showing like duplicate element is : java and next line duplicate element is c.
sir, please provide code for multiple duplicates.
Please, provide an example with Java Streams
Thank u sir..
Welcome
you're explained clearly and easily I understood how to find duplicate elements thank you so much, Sir. But the count of duplicate elements did not get the correct value when using the HashSet approach and I've given my coding below
public class DuplicatesArrays {
public static void main(String[] args) {
Boolean flag = false;
Boolean dntdup = false;
int count = 0;
int count1 = 0;
String [] ar = {"l","m","v","m","n","y","m"};
System.out.println(ar.length);
// using for loop
for (int i = 0; i < ar.length; i++) {
for (int j = i+1; j < ar.length; j++) {
if (ar[i]==ar[j]) {
count = count+1;
System.out.println("found duplicate :" + ar[i]);
flag = false;
}
}
}
System.out.println("count duplicate elements by for loop: "+count);
if (flag == true) {
System.out.println("not found duplicate:");
}
//using hashset
HashSet h = new HashSet();
for (String l : ar) {
if (h.add(l)==false) {
count1 = count1+1;
System.out.println("found duplicate :" + l);
}
}
System.out.println("count duplicate elements by hashset :" + count1);
if (flag == true) {
System.out.println("not found duplicate:");
}
}
}
In hashset the first time the element is found count is zero . So while printing count u should print count + 1
Using HashSet approach, if we have{java, java, java} it will print three times java which is not right, any solution to this?
How it allowing duplicate values to u?
sir along with the finding the duplicate elements i want to print how many times it's repeated...can you help me with this program
suppose if we have multiple duplicates like {"java","python","ruby","c#","python","java"} ??? how to print those duplicates separately?
if you got this code plzz post it ?
@@kanithisatishkumar7036 Iterator i=langs.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
Use Iterator to print all the values in Hashset.
@@baggumani iterator returns all the values in the String including duplicates, not only the duplicates
How can I get the count of this duplicate element
+1
@@omprakash7040 it didn't work like that, can you please share the code
@@tarunsharma9974 First intinalize count as 0 after printing the duplicate elements increment the count.
How to remove duplicate duplicate values from array ??
use Set
Create a new array and add the elements only when Not duplicate. Or use a java collection type that ignores duplicates
Can some body explain; on first logic first element is compared with elements after that, so say 3 rd element is compared with 4th element, it is not compared with first or second, Is that logic right???
No need coz duplicate off first nd second already searched
Thats not needed when you search,i=1. And j=3 ,the same thing need to be searched in j=3 and i=1
Not needed because i=0, j=1 (arr[0]== arr[1]) comparison is already done to check for duplicates. So, there is no need of comparing again with the former i.e; i=1, j=0 arr[1] == arr[0]
Take the upper for loop once and check what it is printing ok
Then check what the lower for loop checking
Now put i = let's say 4
And then u put 2nd for loop you will see it will give values upto 4 ok so it always checks values hoto that point
Sir your mail is there?? I have some doubts??
The first logic wont work well as it does not compare with the previous values
what about multiple duplicates
❤️
the methods are time complexity are really bad. the 1st one is O(N^2).
you can do it in O(N) with 1 loop and check for 3 or more duplicates/
the code:(same for String)
class FindDuplicate {
// Function to print duplicates
void printRepeating(int arr[], int size)
{
int i;
System.out.println("The repeating elements are : ");
for (i = 0; i < size; i++) {
int j = Math.abs(arr[i]);
if (arr[j] >= 0)
arr[j] = -arr[j];
else
System.out.print(j + " ");
}
}
// Driver code
public static void main(String[] args)
{
FindDuplicate duplicate = new FindDuplicate();
int arr[] = { 1, 2, 3, 1, 3, 6, 6 };
int arr_size = arr.length;
duplicate.printRepeating(arr, arr_size);
}
}
for String:
String[] users = "User1,User2,User1,User,User".split(",");
Set uniquUsers = new HashSet();
for (int i = 0; i < users.length; i++) {
if (!uniquUsers.add(users[i]))
users[i] = "Duplicate"; // here I am assigning Duplicate instead if find duplicate
// you can assign as null or whatever you want to do with duplicates.
}
System.out.println(Arrays.toString(users));
The first one is wrong algo
@@gautamkumarjha2686 it's not wrong google in geeksforgeeks
@@HvujES that code will true on some particular arrey only
For example any integer greater then 15 at any index of array
@@gautamkumarjha2686 works fine for me.
www.geeksforgeeks.org/find-duplicates-in-on-time-and-constant-extra-space/
suppose we have multiple duplicates like below and want to the print the output only once like duplicate elements = java,c++,python. Could you please guide me.
String arr[] = {"java","python","c++","java","c","c++","devops","java","c++","python","python"};