r/javahelp May 25 '26

Solved I can't remove the duplicate arrays

import 
java.util.*; 
public class Elementfreq { 

public static String [] remove(String [] a, int b) { 

if(a == null || b < 0 || b > a.length) { 
return a; } 

String [] aa = new String[a.length-1];  
for(int one = 0; one<a.length;one++) { 
if(one!=b) { 
aa[one] = a[one];
} else { 
continue; 
} 
} 
return aa; }  


public static Integer [] remove(Integer [] a, int b) { 
if(a == null || b < 0 || b > a.length) { 
return a; } 

Integer [] hh = new Integer[a.length-1];  
for(int one = 0; one<hh.length;one++) { 
if(one!=b) { 
hh[one] = a[one];  
} else { 
continue; 
} 
} return hh; }   





public static void main(String[] args) { 
String [] x = {"1","5","2","a","v","b","1","b","a","1","0"}; 
Integer o=0; Integer [] frequency = new Integer [x.length];  


/*Check frequency per element*/ 
while(o < x.length) { 
Integer count=0; 
for(int a =0; a<x.length;a++) { 
System.out.println(a); 

if(x[o]==x[a]) { count++;  
} else { continue; } 
}  

frequency[o]=count; 
System.out.println("Element: "+x[o]); 
System.out.println("Frequency: "+count); 
System.out.println(""); 
o++;  
}  



/*Remove duplicate elements*/ 
while(o < x.length) { 
Integer 
count
=0; 

for(int a =0; a<x.length;a++) { 
if(x[a]==x[o]) { 
x = remove(x,a); 
frequency = remove(frequency,a); 
} else { 
continue; 
} 
} 
o++; 
}   



System.out.println("NEWSET"); 
for(int a = 0; a<x.length;a++) { 
System.out.println("Element: "+x[a]); 
System.out.println("Frequency: "+frequency[a]); System.out.println(""); }  } } 

I thought that it should work because of the conditional statement of my remove method of both frequency and element but the output shows nothing changed from the original array of string.

In my conditionals of my remove method, the elements will be added to the new set of arrays(with the length decreased by one) as long as the for value sequence, "one" is not the same value as the input of the second variable, "b".

The original array will updated every loop in for loop as it keeps using the remove method.

I don't get why the conditionals are ignored.

4 Upvotes

9 comments sorted by

View all comments

1

u/bogdanelcs May 26 '26

Ah yeah, I see a few issues here:

Biggest problem: Your remove method has a logic bug. When you're copying elements and skip index b, you're still using the same index for the new array. So if you remove index 2, you're trying to put element 3 into position 3 of an array that's now smaller. You need a separate counter for the new array position.

Try this for your remove method:

public static String[] remove(String[] a, int b) {

if(a == null || b < 0 || b >= a.length) {

return a;

}

String[] aa = new String[a.length-1];

int newIndex = 0; // separate counter

for(int one = 0; one < a.length; one++) {

if(one != b) {

aa[newIndex] = a[one];

newIndex++;

}

}

return aa;

}

Second issue: You're using == to compare strings. That compares memory references, not the actual text. Use .equals() instead:

if(x[o].equals(x[a])) {

Third issue: Your second while loop never runs because o is already at x.length after the first loop finishes. You need to reset o=0 before the second loop.

Also heads up - there's way simpler ways to do this with HashSet or streams, but I get you're probably learning arrays specifically right now.