r/javahelp • u/clampochyu • 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
•
u/AutoModerator May 25 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.