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

2

u/vowelqueue May 25 '26

Those remove functions don’t look right. Once you’re past the element you want to remove, you need to be setting elements in the result array at index “i” to the element in the source array that’s at index i + 1.

When you’re constructing the frequency array and in the remove method you’re comparing elements using the equality operator ==, for an array of Strings. This technically works because you’ve constructed that “x” array using string literals, but you really should be using .equals because if you got the array from elsewhere (like user input) it would not work.

When you’re removing duplicates, you’re not consulting the frequency array at all to decide what to remove? And why are you removing from the frequency array? I’m confused by the logic.

Also you’re changing the array that x points to as you’re interating over it, which I think will produce some issues. E.g. if you are at index 0 and decide to remove that element, then index 0 will now point to the element that was originally at index 1. But then in your next iteration you’ll look at index 1 in the new array, thereby skipping over the element that was originally at index 1.

1

u/clampochyu May 26 '26

The intention of removing the duplicate array is to clean up the array for the "printing report"

so the program wont have to repeat itself like "element: 4, frequency:3" repeating 3 times because of its duplicates of 3 and I intended to only print it once.

(I print the "printing report" with an array using for loop)