Frequently Asked Java Program 19: How To Find Duplicate Elements in Array

  Рет қаралды 203,958

SDET- QA

SDET- QA

Күн бұрын

Topic : How To Find Duplicate Elements in Array
#########################
Udemy Courses:
#########################
Manual Testing+Agile with Jira Tool
************************************
► shorturl.at/xNXZ4
Selenium with Java+Cucumber
********************************
► shorturl.at/aIKMW
Selenium with Python & PyTest
********************************
► shorturl.at/ELW09
Selenium with python using Robot framework
****************************************
► shorturl.at/bcDPZ
API Testing(Postman, RestAssured & SoapUI)
*****************************************
► shorturl.at/uAWY7
Web & API Automation using Cypress with Javascript
********************************************
► rb.gy/4uum2n
Playwright with Javascript
**************************
► shorturl.at/gALZ1
Jmeter-Performance Testing
************************
► rb.gy/ocj7r2
SDET Essencials(Full Stack QA)
*************************
► rb.gy/hlnf2v
Appium-Mobile Automation Testing
************************************
► rb.gy/wmzv64
Java Collections
*****************
► bit.ly/3S66ezE
Python Programming
*********************
► urlis.net/gdsf...
Cucumber BDD Framework
***************************
► bit.ly/3F7d69r
Protractor with Javascript
***************************
► urlis.net/4udd...
####################################
KZbin Playlists:
####################################
Manual Testing & Agile
***********************
► bit.ly/3ujvaYa
► bit.ly/3OYzxQj
SQL
*************************
► bit.ly/3R6XjeG
► bit.ly/3nE0a1s
► bit.ly/3IeKuuJ
linux & Shell Scripting
**********************
► bit.ly/3up1Z6a
► bit.ly/3nzvGxx
Java
**********************
► bit.ly/3R9QfOs
► bit.ly/3AoRMKM
► bit.ly/3IbTTDn
► bit.ly/3ybXInv
► bit.ly/3yCoHdw
Selenium With Java+Cucumber
********************************
► bit.ly/3P2FMm4
► bit.ly/3yhiIt4
► bit.ly/3uokzet
► bit.ly/3a7XIND
► bit.ly/3umN2S9
► bit.ly/3aar7GW
► bit.ly/3y9kD2S
► bit.ly/3uhLskn
Python
********************************
► bit.ly/3agNgU0
► bit.ly/3NJNnoy
► bit.ly/2CTQ4mR
► bit.ly/3OIYWh1
Selenium With Python,Pytest&Behave
***************************************
► bit.ly/3OHHoC9
► bit.ly/3IeNLdv
► bit.ly/2J4tPeT
► bit.ly/3ydSkAq
Selenium With Python Using Robert Framework
(Web&API Testing)
*************************************************
► bit.ly/3nUvlpr
► bit.ly/3nUvE3z
API Testing (Postman,SoapUi,&Rest Assured)
**********************************************
► bit.ly/3OXacWY
► bit.ly/3yh0UhE
► bit.ly/3nC9DWQ
► bit.ly/3yGfhNS
► bit.ly/3OJa11H
► bit.ly/3P2slCC
Mobile App Testing Appium
****************************
► bit.ly/3Al49HG
Performance Testing Jmeter
*******************************
► bit.ly/3nz4fE7
Maven,Jenkins,Git,Github,CI/CD
*******************************
► bit.ly/3P16HPj
► bit.ly/3yhlUVA
► bit.ly/3afYfwT
► bit.ly/3yGzweG
► bit.ly/3yF73Ws
► bit.ly/3NCOmH7
SQL,DB Testing&ETL,Bigdata
*******************************
► bit.ly/3NKOcNY
► bit.ly/3OVpDyI
► bit.ly/3NEvUy0
► bit.ly/3IryxlT
► bit.ly/3RkWBLh
JavaScript Based Automation Tools
********************************
► bit.ly/3nUx51X
► bit.ly/3AqI6Q0
► bit.ly/3yeurbT
► bit.ly/3ujzpTK
Selector Hub Tools
********************
►rb.gy/h3h424
GraphQL
******************
► rb.gy/sj3gys
Cypress API Testing
********************
► shorturl.at/fjKPX
Cypress Web Testing
**********************
► shorturl.at/agjlK
Playwright with Javascipt
**************************
► shorturl.at/adSTY
#PythonDuplicates
#ArrayDuplicates
#DuplicateElements
#ArrayManipulation
#CodingChallenge
#PythonProgramming
#AlgorithmPractice
#TechInterview
#CodingProblems
#DataStructures
#ArrayOperations
#PythonArrays
#ArrayAlgorithms
#ProgrammingTips
#CodeSolutions
#InterviewPrep
#PythonTips
#ProblemSolving
#ArrayManipulation
#CodeSnippets
#PythonTricks
#AlgorithmDesign
#ArraySearching
#PythonCoding
#TechInterviewPrep
#CodingInPython
#ProgrammingLogic
#CodeExamples
#DebuggingSkills
#ArrayChallenges
#PythonSkills
#CodingCommunity
#AlgorithmProblems
#ArrayProblems
#CodingJourney
#PythonLearning
#ProblemSolvingInPython
#ArrayLogic
#CodingHelp
#PythonDevelopment
#AlgorithmicThinking
#ArrayInterview
#CodingExperience
#PythonArrays101
#CodeDebugging
#AlgorithmExplained
#ArrayExploration
#PythonCodeSnippets
#CodingExplained
#DuplicateFinding

Пікірлер: 59
@bhushanmali2598
@bhushanmali2598 Жыл бұрын
Finally i found this in a very perfect way of explanation. Thank you ❤️
@sdetpavan
@sdetpavan Жыл бұрын
Welcome
@divyad9144
@divyad9144 2 жыл бұрын
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
@Testology-PradeepPandey
@Testology-PradeepPandey 2 жыл бұрын
Hahaha...asking or telling
@msgudaduramath4980
@msgudaduramath4980 Жыл бұрын
It is not nessesary to compare the last element with others because it is already compared by other elements
@samK12_18
@samK12_18 2 жыл бұрын
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
@bhagathgudivaka1520
@bhagathgudivaka1520 2 жыл бұрын
i
@samK12_18
@samK12_18 2 жыл бұрын
@@bhagathgudivaka1520 Yes because if i goes to 5, then j becomes 6 and it will return array index out of bounds error.
@bhagathgudivaka1520
@bhagathgudivaka1520 2 жыл бұрын
@@samK12_18 if j becomes 6 then inner loop will be stopped and Comes to outer loop again
@sarathr4495
@sarathr4495 Жыл бұрын
Hash set way is life saver sir 👍
@sdetpavan
@sdetpavan Жыл бұрын
My pleasure
@gauri992
@gauri992 3 жыл бұрын
Ur the best Sir. thanku
@sdetpavan
@sdetpavan 2 жыл бұрын
Welcome
@snehalwakode5291
@snehalwakode5291 Жыл бұрын
Thank u sir..
@sdetpavan
@sdetpavan Жыл бұрын
Welcome
@TheShubhamVsnv
@TheShubhamVsnv Жыл бұрын
?? how can fix this if i have three same element ?? arr[]={"java","c","c++","python","java","java"}; output : java java java
@World90yes
@World90yes Жыл бұрын
Same error
@World90yes
@World90yes Жыл бұрын
Same error
@Johnsahu-m7k
@Johnsahu-m7k 10 ай бұрын
great sir. plz upload latest trending logical programs java in automation interview.
@kanithisatishkumar7036
@kanithisatishkumar7036 3 жыл бұрын
sir, please provide code for multiple duplicates.
@srinivasrao-lw6xk
@srinivasrao-lw6xk 3 жыл бұрын
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.
@BharathKumarEnishetty
@BharathKumarEnishetty Жыл бұрын
we can directly write as if(flag) , it would look good right , though it is fine , thanks
@sridhar6358
@sridhar6358 3 жыл бұрын
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
@dsantoshini
@dsantoshini 2 жыл бұрын
yes
@keshavrawal2746
@keshavrawal2746 2 жыл бұрын
it works as you said bcoz in first iteration "java" is already compared with all the elements
@MDALAM-yh3pf
@MDALAM-yh3pf Жыл бұрын
first java is small j , last 2 java is capital J; how you prove multiple entry give same result?
@thesurajpatade
@thesurajpatade 2 жыл бұрын
Using HashSet approach, if we have{java, java, java} it will print three times java which is not right, any solution to this?
@csRHarish
@csRHarish 2 жыл бұрын
How it allowing duplicate values to u?
@silvere4830
@silvere4830 2 жыл бұрын
Please, provide an example with Java Streams
@user-mg8mq8tr7z
@user-mg8mq8tr7z Жыл бұрын
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
@suiiiiii5798
@suiiiiii5798 Ай бұрын
❤️
@jarvisfriday7452
@jarvisfriday7452 4 жыл бұрын
suppose if we have multiple duplicates like {"java","python","ruby","c#","python","java"} ??? how to print those duplicates separately?
@kanithisatishkumar7036
@kanithisatishkumar7036 3 жыл бұрын
if you got this code plzz post it ?
@baggumani
@baggumani 2 жыл бұрын
@@kanithisatishkumar7036 Iterator i=langs.iterator(); while(i.hasNext()) { System.out.println(i.next()); } Use Iterator to print all the values in Hashset.
@sarves2482
@sarves2482 6 ай бұрын
@@baggumani iterator returns all the values in the String including duplicates, not only the duplicates
@indiantourism975
@indiantourism975 Жыл бұрын
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:"); } } }
@Forty3Stories
@Forty3Stories Жыл бұрын
In hashset the first time the element is found count is zero . So while printing count u should print count + 1
@tarunsharma9974
@tarunsharma9974 2 жыл бұрын
How can I get the count of this duplicate element
@omprakash7040
@omprakash7040 2 жыл бұрын
+1
@tarunsharma9974
@tarunsharma9974 2 жыл бұрын
@@omprakash7040 it didn't work like that, can you please share the code
@gurunathkulkarni-ry8yv
@gurunathkulkarni-ry8yv Жыл бұрын
​@@tarunsharma9974 First intinalize count as 0 after printing the duplicate elements increment the count.
@as8212
@as8212 4 жыл бұрын
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???
@Mm-zt1gk
@Mm-zt1gk 4 жыл бұрын
No need coz duplicate off first nd second already searched
@renjithdhoni7
@renjithdhoni7 4 жыл бұрын
Thats not needed when you search,i=1. And j=3 ,the same thing need to be searched in j=3 and i=1
@bharathkoneru4008
@bharathkoneru4008 4 жыл бұрын
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]
@harish3787
@harish3787 3 жыл бұрын
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
@faisalsakib9586
@faisalsakib9586 4 жыл бұрын
How to remove duplicate duplicate values from array ??
@theflutterboi
@theflutterboi 3 жыл бұрын
use Set
@suganthym1438
@suganthym1438 3 жыл бұрын
Create a new array and add the elements only when Not duplicate. Or use a java collection type that ignores duplicates
@maharudra947
@maharudra947 Жыл бұрын
what about multiple duplicates
@sureshsubramaniyan3201
@sureshsubramaniyan3201 2 жыл бұрын
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"};
@sridhar6358
@sridhar6358 3 жыл бұрын
The first logic wont work well as it does not compare with the previous values
@mohammedajazquadri7869
@mohammedajazquadri7869 4 жыл бұрын
Sir your mail is there?? I have some doubts??
@Bulldoges90
@Bulldoges90 3 жыл бұрын
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));
@gautamkumarjha2686
@gautamkumarjha2686 3 жыл бұрын
The first one is wrong algo
@Bulldoges90
@Bulldoges90 3 жыл бұрын
@@gautamkumarjha2686 it's not wrong google in geeksforgeeks
@gautamkumarjha2686
@gautamkumarjha2686 3 жыл бұрын
@@Bulldoges90 that code will true on some particular arrey only For example any integer greater then 15 at any index of array
@Bulldoges90
@Bulldoges90 3 жыл бұрын
@@gautamkumarjha2686 works fine for me. www.geeksforgeeks.org/find-duplicates-in-on-time-and-constant-extra-space/
Remove Duplicates from sorted Array
21:22
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 169 М.
I Took a LUNCHBAR OFF A Poster 🤯 #shorts
00:17
Wian
Рет қаралды 15 МЛН
How to Find Duplicates Elements in Java Array? - Java Interview Questions -5
29:34
Java Arrays : Understanding and Using Arrays in Java
37:33
Ashok IT
Рет қаралды 9 М.
Solve Any Pattern Question With This Trick!
57:20
Kunal Kushwaha
Рет қаралды 2,4 МЛН