Java 从整数数组中删除N个重复项
问题陈述:我必须从数组中删除n个重复项 以下是完整的问题陈述: 我的解决方案是:Java 从整数数组中删除N个重复项,java,arrays,Java,Arrays,问题陈述:我必须从数组中删除n个重复项 以下是完整的问题陈述: 我的解决方案是: public class minion_labour_shift_2ndTry { static int[] data = {1,2, 2, 3, 3, 3, 4, 5, 5}; public static void main(String[] args) { Scanner reader = new Scanner(System.in); int n =
public class minion_labour_shift_2ndTry {
static int[] data = {1,2, 2, 3, 3, 3, 4, 5, 5};
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
data = answer(data, n);
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + " ");
}
}
public static int[] answer(int[] data, int n) {
if (data.length>99){
System.exit(0);
}
int[] result = new int[99];
ArrayList<Integer> temp = new ArrayList<>();
int counter = 0, count ,maxCount = 0;
for (int i = 0; i < data.length; i++) {
boolean isDistinct = false;
for (int j = 0; j < i; j++) {
if (data[i] == data[j]) {
isDistinct = true;
break;
}
}
if (!isDistinct) {
result[counter++] = data[i];
}
}
for (int i = 0; i < counter; i++) {
count = 0;
for (int j = 0; j < data.length; j++) {
if (result[i] == data[j]) {
count++;
}
}
System.out.println("....... count"+count);
if (maxCount <= count){
maxCount = count;
}
if (count <= n){
temp.add(result[i]);
}
}
if (maxCount-1 < n){
return data;
}
data = new int[temp.size()];
for (int i = 0; i <temp.size() ; i++) {
data[i] = temp.get(i);
}
return data;
}
}
public class minion\u labour\u shift\u 2ndTry{
静态int[]数据={1,2,2,3,3,3,4,5,5};
公共静态void main(字符串[]args){
扫描仪阅读器=新扫描仪(System.in);
int n=reader.nextInt();
数据=答案(数据,n);
对于(int i=0;i99){
系统出口(0);
}
int[]结果=新的int[99];
ArrayList temp=新的ArrayList();
int计数器=0,计数,最大计数=0;
对于(int i=0;i 如果(maxCount不打算提供完整的解决方案,但由于不清楚您在做什么而建议重新修改算法,则您从未解释过您对算法的实际想法。例如,您使用isDistinct的目的是什么
1) 循环一次并计算每个数字的频率。您可以使用长度为100的数组,因为这就是所有的数据输入。在循环过程中,跟踪两件事:出现n次以上的条目总数,以及这些数字是什么
2) 创建一个适当大小的结果数组(根据上面的计算),然后再次循环遍历列表,并填充未超过阈值的元素。我最初误读了需求,这会执行要求:
public static int[] answer(int[] data, int n) {
Map<Integer, Integer> counts = new HashMap<>();
int elementsNeeded = 0;
for (int i = 0; i < data.length; i++) {
Integer currentCount = counts.get(data[i]);
currentCount = currentCount == null ? 1 : ++currentCount;
counts.put(data[i], currentCount);
if (currentCount <= n + 1) {
elementsNeeded += currentCount > n ? -n : 1;
}
}
int[] resultArray = new int[elementsNeeded];
int j = 0;
for (int i = 0; i < data.length; i++) {
if (counts.get(data[i]) <= n) {
resultArray[j++] = data[i];
}
}
return resultArray;
}
公共静态int[]应答(int[]数据,int n){
映射计数=新的HashMap();
int ELEMENTSNEEED=0;
对于(int i=0;ianswer(data,2)
返回[1,2,4,5]
,但应返回[1,2,2,4,5,5]
。
public static int[] answer2(int[] data, int n) {
if (data.length>99){
System.exit(0);
}
ArrayList<Integer> temp = new ArrayList<>();
int count;
for (int i = 0; i < data.length; i++) {
count = 0;
for (int j = 0; j < data.length; j++) {
if (data[i] == data[j]) {
count++;
}
}
if (count <= n){
temp.add(data[i]);
}
}
data = new int[temp.size()];
for (int i = 0; i <temp.size() ; i++) {
data[i] = temp.get(i);
}
return data;
}