Duplicates in String array vs Int array | Selenium Forum
M
Posted on 12/07/2016
[b:mmonsopz]To find duplicates in Int array[/b:mmonsopz]

int i[]=new int[]{546,123,456,546,123,855,588,989,456};

for(int ab=0;ab<i.length;ab++){

for( int ac=ab+1; ac<i.length;ac++){

if(i[ab]==i[ac]){

System.out.println(i[ab] +" "+ "is duplicate value");

[b:mmonsopz]To find duplicates in String array[/b:mmonsopz]

String str[]=new String[]{"abc","def","xyz","abc","def"};

System.out.println(str.length);

for(int i=0; i<str.length;i++){

for(int k=i+1;k<str.length;k++){

if(str[i].equals(str[k])){

System.out.println("duplicate value is "+" "+ str[i]);

In int array, "==" is used to compare 2 values in for loop where as in String array to compare ".equals" is used.

Is this correct way or there is any better solution?

M
Replied on 12/07/2016

seems fine. is it working properly?


M
Replied on 12/07/2016

Yes. it is working fine. But for int array, "==" is used to compare 2 values in for loop where as in String array to compare ".equals" is used. Is this correct logic or is there any better solution?


M
Replied on 12/07/2016

yes it is correct.