Saturday, February 25, 2023
HomeSoftware EngineeringDelete Occurrences of an Ingredient if it happens greater than N instances...

# Delete Occurrences of an Ingredient if it happens greater than N instances in Java

## The problem#

Given a listing lst and a quantity N, create a brand new checklist that comprises every variety of lst at most N instances with out reordering. For instance if N = 2, and the enter is [1,2,3,1,2,1,2,3], you are taking [1,2,3,1,2], drop the following [1,2] since this is able to result in 1 and a couple of being within the end result 3 instances, after which take 3, which results in [1,2,3,1,2,3].

Instances

``````// return [20,37,21]
EnoughIsEnough.deleteNth(new int[] {20,37,20,21}, 1)

// return [1, 1, 3, 3, 7, 2, 2, 2]
EnoughIsEnough.deleteNth(new int[] {1,1,3,3,7,2,2,2,2}, 3)
``````

## The answer in Java code#

Possibility 1:

``````import java.util.Arrays;
import java.util.HashMap;

public class EnoughIsEnough {
public static int[] deleteNth(int[] parts, int maxOcurrences) {
HashMap<Integer, Integer> map = new HashMap<>();
return Arrays.stream(parts)
.filter(i -> {
map.merge(i, 1, Integer::sum);
return map.get(i) <= maxOcurrences;
})
.toArray();
}
}
``````

Possibility 2:

``````import java.util.*;

public class EnoughIsEnough {
public static int[] deleteNth(int[] parts, int maxOcurrences) {
if (parts == null || parts.size == 0) return new int[0];
Map<Integer, Integer> numberCount = new HashMap<>();
Listing<Integer> filteredList = new ArrayList<>();
for (Integer aNumber : parts) {
if (numberCount.containsKey(aNumber))
numberCount.put(aNumber, numberCount.get(aNumber)+1);
else numberCount.put(aNumber, 1);
if (numberCount.get(aNumber) <= maxOcurrences)
}
return filteredList.stream().mapToInt(Integer::valueOf).toArray();
}
}
``````

Possibility 3:

``````import java.util.Collections;
import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.Listing;

public class EnoughIsEnough {
public static int[] deleteNth(int[] ar, int max) {
Listing<Integer> checklist = new ArrayList<>();
for (int n : ar)
if (Collections.frequency(checklist, n) < max) checklist.add(n);
return checklist.stream().mapToInt(Integer::intValue).toArray();
}
}
``````

## Check instances to validate our answer#

``````import org.junit.jupiter.api.Check;
import static org.junit.jupiter.api.Assertions.*;

class EnoughIsEnoughTest {
@Check
public void deleteNth() throws Exception {
assertArrayEquals(
new int[] { 20, 37, 21 },
EnoughIsEnough.deleteNth( new int[] { 20, 37, 20, 21 }, 1 )
);
assertArrayEquals(
new int[] { 1, 1, 3, 3, 7, 2, 2, 2 },
EnoughIsEnough.deleteNth( new int[] { 1, 1, 3, 3, 7, 2, 2, 2, 2 }, 3 )
);
assertArrayEquals(
new int[] { 1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5 },
EnoughIsEnough.deleteNth( new int[] { 1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1 }, 3 )
);
assertArrayEquals(
new int[] { 1, 1, 1, 1, 1 },
EnoughIsEnough.deleteNth( new int[] { 1, 1, 1, 1, 1 }, 5 )
);
assertArrayEquals(
new int[] { },
EnoughIsEnough.deleteNth( new int[] { }, 5 )
);
}
}
``````

RELATED ARTICLES