I need to write a method where a int[] will be supplied as an input and it should return an array, but with all of the numbers that occur more than n times removed entirely.
Inputs:
(int list) data = [1, 2, 2, 3, 3, 3, 4, 5, 5]
Output:
(int list) [1, 4]
These are the steps i have tried.
Copy the int array to ArrayList (inputList).
Create a LinkedHashset to find unique values
Iterate LH and find the collection frequency of ArrayList with iterator.
int[] intArray = new int[0];
if(n!=0){
if(data.length<100){
ArrayList<Integer> inputList = new ArrayList<Integer>(data.length);
//System.out.println(Arrays.toString(data));
for (int i = 0; i < data.length; i++){
inputList.add(Integer.valueOf(data[i]));
}
LinkedHashSet<Integer> lhs = new LinkedHashSet<>(inputList);
intArray = new int[lhs.size()];
int i=0;
int j=0;
Iterator<Integer> itr = lhs.iterator();
while(itr.hasNext()){
Integer shiftNumber = itr.next();
if(Collections.frequency(inputList, shiftNumber)==1) {
intArray[i++] = shiftNumber.intValue();
j++;
}
}
intArray = Arrays.copyOf(intArray, j);
return intArray;
}
}
return intArray;
I am able to achieve the results with the above snippet.However, I need suggestions in reducing the piece of code and improving performance by using any algorithms or other collection objects.
You could use a map instead.
The map key would represent the values found in the array; the map value would be a counter.
You iterate your array, and for each element you either put a counter=1 (when finding that value the first time); or you simply increase that counter.
Finally, you collect only those map keys that show a counter value of 1.
You are likely overcomplicating the algorithm. It might be simpler to just map each value to its frequency and then copy those values with frequency less than n. Also note that you don't need to explicitly covert between int and Integer: Java does it for you automatically.
int[] output = new int[input.length];
Map<Integer,Integer> counts = new HashMap<>();
int size = 0;
for (int i = 0; i < input.length; i++) {
counts.put(input[i], counts.getOrDefault(input[i], 0) + 1);
}
for (int i = 0; i < input.length; i++) {
if (counts.get(input[i]) < n)
output[size++] = input[i];
}
return Arrays.copyOf(output, size);
If you are familiar with Java 8 streams then the code can be substantially reduced:
Map<Integer,Integer> count = Arrays.stream(input).boxed()
.collect(groupingBy(identity(), counting()));
return Arrays.stream(input).filter(i -> count.get(i) < n).toArray();
Related
I'm trying to get the N smallest numbers (given by the user) in an array without using methods like sort()... in the last step, I keep getting only the smallest values and 0 for the rest.. where's the problem?
//1- Scanner to take inputs
Scanner input = new Scanner(System.in);
//2- Take the array size as input and store it in "sizeOfArr" var
System.out.print("Enter the array size: ");
int sizeOfArr = input.nextInt();
//3- Assign the input as an array size
int array[] = new int[sizeOfArr];
//4- Looping on the array and update its values by inputs taken from the user
for(int i = 0; i < array.length; i++) {
System.out.print("Enter "+ (i+1) + "-st element: ");
array[i] = input.nextInt();
}
//5- Print out the array after convert it to String
System.out.println(Arrays.toString(array));
//6- Find the smallest element in the array and print it
int minVal = array[0];
for(int i = 0; i < array.length; i++) {
if (array[i] < minVal) {
minVal = array[i];
}
}
// System.out.println(minVal);
//7- Find the (n) smallest of number defined by the user
System.out.print("Enter the number of smallest numbers do you want: ");
int n = input.nextInt();
//8- new array to store n smallest numbers
int smallestNums[] = new int[n];
//9- trying to loop on the original array n times
int counter;
for(int i = 0; i < n ; i++) {
//10- trying to loop on the original array to store the smallest values in smallestNum[] array.
for(int j = 0; j < array.length; j++) {
smallestNums[i] = minVal;
}
if(smallestNums[i] == smallestNums[i]) {
break;
}
}
System.out.println(Arrays.toString(smallestNums));
Here is one way. Just do a partial sort with the outer loop limit equal to the number of items required. This is variant of the selection sort. This example, varies n in the outer list for demo purposes.
int[] array = { 10, 1, 5, 8, 7, 6, 3 };
for (int n = 1; n <= array.length; n++) {
int[] smallest = getNSmallest(n, array);
System.out.printf("smallest %2d = %s%n", n,
Arrays.toString(smallest));
}
prints
smallest 1 = [1]
smallest 2 = [1, 3]
smallest 3 = [1, 3, 5]
smallest 4 = [1, 3, 5, 6]
smallest 5 = [1, 3, 5, 6, 7]
smallest 6 = [1, 3, 5, 6, 7, 8]
smallest 7 = [1, 3, 5, 6, 7, 8, 10]
Here is the method. The first thing to do is copy the array so the
original is preserved. Then just do the sort and return array of smallest elements.
public static int[] getNSmallest(int n, int[] arr) {
int[] ar = Arrays.copyOf(arr, arr.length);
int[] smallest = new int[n];
for (int i = 0; i < n; i++) {
for (int k = i + 1; k < ar.length; k++) {
if (ar[i] > ar[k]) {
int t = ar[i];
ar[i] = ar[k];
ar[k] = t;
}
}
smallest[i] = ar[i];
}
return smallest;
}
For this task, you don't have to sort the whole array. Only a group of N elements has to be sorted. I.e. only a partial sorting is required.
Below, I've provided two implementations for this problem. The first utilizes only plane arrays and loops, the second makes use of the PriorytyQueue.
The first solution maintains a variable pos which denotes the position in the result array which isn't assigned yet. Note that the default value for an element of the int[] is 0. It's important to be able to distinguish between the default value and a zero-element from the given array. Hence we can't rely on the values and have to track the number of elements that are assigned.
Every element of the source array gets compared with all the elements of the result array that are already assigned. The new element will be added to the result array in two cases:
nested loop has reached an unoccupied position pos in the result array;
an element in the result array that is greater than the next element from the given array has been found.
In the first case, a new element gets assigned the position denoted by pos. In the second case, a new element has to be inserted
nested loop iterates over the given array at the current position i and all elements must be shifted to the right. That's what the method shiftElements() does.
The First solution - Arrays & Loops
public static int[] getSmallest(int[] arr, int limit) {
int[] result = new int[Math.min(limit, arr.length)];
int pos = 0;
for (int next: arr) {
for (int i = 0; i < Math.min(pos + 1, result.length); i++) {
if (i == pos) result[i] = next;
else if (result[i] > next) {
shiftElements(result, next, i, Math.min(pos + 1, result.length));
break;
}
}
pos++;
}
return result;
}
private static void shiftElements(int[] arr, int val, int start, int end) {
int move = arr[start];
arr[start] = val;
for (int i = start + 1; i < end; i++) {
int temp = arr[i];
arr[i] = move;
move = temp;
}
}
Maybe you'll be more comfortable with the first version, but if you are somehow familiar with the Collections framework, then it's a good time to get acquainted with PriorytyQueue. In the nutshell, this collection is backed by an array and maintains its element in the same order as they were added, but when an element is being deleted collection retrieves the smallest one according to the natural order or based on the Comparator, which can be provided while instantiating the PriorytyQueue. It uses a sorting algorithm that is called a heapsort which allows removing a single element in O(log N) time.
The Second solution - PriorytyQueue
public static int[] getSmallestWithPriorityQueue(int[] arr, int limit) {
Queue<Integer> queue = new PriorityQueue<>();
populateQueue(queue, arr);
int[] result = new int[Math.min(limit, arr.length)];
for (int i = 0; i < result.length; i++) {
result[i] = queue.remove();
}
return result;
}
private static void populateQueue(Queue<Integer> queue, int[] arr) {
for (int next: arr) {
queue.add(next);
}
}
main & utility-method to generate an array
public static void main(String[] args) {
int[] source = generateArr(100, 10);
System.out.println("source : " + Arrays.toString(source));
int[] result1 = getSmallest(source, 3);
System.out.println("result(Arrays & Loops) : " + Arrays.toString(result1));
int[] result2 = getSmallestWithPriorityQueue(source, 3);
System.out.println("result(PriorityQueue) : " + Arrays.toString(result2));
}
public static int[] generateArr(int maxVal, int limit) {
Random random = new Random();
return IntStream.generate(() -> random.nextInt(maxVal + 1))
.limit(limit)
.toArray();
}
output
source : [61, 67, 78, 53, 74, 51, 50, 83, 59, 21]
result(Arrays & Loops) : [21, 50, 51]
result(PriorityQueue) : [21, 50, 51]
Randomized select allows to find k-th ranked element in linear time on average.
It alters the input order, so practically, it makes sense to just sort and return k-th element of the sorted array. Especially if there are several such calls on the given input array.
I currently need to code a program that does a pairwise comparison to find matching pairs in an int list, but the only way I can think of doing it is through a nested for loop. Is there a way to make the time complexity O(n) instead of the O(n^2) of a nested for loop?
int[] arr = new int[n];
int total = 0;
for (int i=0; i<n; i++){
for (int j=i+1; j<n; j++){
if (arr[i] == arr[j]){
total++;
}
}
}
Looks like you are only worried about the count. So, if modifying an array is not an issue then, apply quick sort in O(nlog(n)) and then count the neighbours in O(n).
You can use a HashSet which has O(1) complexity of contains method - because hashCode of Integer value in Java is well distributed (it's just a value of the Integer) you should have constant complexity always
HashSet<Integer> set = new HashSet();
for (int i=0; i<n; i++) {
if(set.contains(arr[i])) {
total++;
} else {
set.add(arr[i]);
}
}
Read more here:
HashSet look-up complexity?
There's also one additional algorithm that could be implemented here but it require some data limitations (due to the max length of array on your machine).
You could just create an int array yourarray initialized with 0 with a length of max(arr) + 1. Then you are iterating over arr every time executing yourarray[arr[i]]++ and then you are iterating over yourarray checking where the value is greater than 1 - if it this then it means that this value has to repeat
O(n) solution.
Try this.
public static void main(String[] args) {
int[] arr = {0, 1, 2, 3, 4, 3, 2, 3};
int total = arr.length - (int)IntStream.of(arr).distinct().count();
System.out.println(total);
}
output:
3
I think you can use a HashSet to resolve this problem. The HashSet in JAVA doesn't allow insert into a duplicate element. So you can use it to making the time complexity become O(n).
int[] arr = new int[n];
Set<Integer> set = new HashSet<Integer>();
Integer total = 0;
for (int x: arr)
{
if (!set.add(x))
{
total++;
}
}
I'm trying to loop through numbers from -6 to 38, and output the odd numbers into an array. I don't know how to store the output into an array.
for (int i = -6; i<38; i++){
if (i%2!=0){
**output to array**
}
}
Because we are not able to know how many the number of the odd numbers is, so you can use IntStream to fix this issue if your java version is 8 or above.
int[] array = IntStream.range(-6, 38).filter(x -> x % 2 != 0).toArray();
Or you can use ArrayList
List<Integer> list = new ArrayList<>();
for (int i = -6; i < 38; i++) {
if (i % 2 != 0) {
list.add(i);
}
}
As the number to elements in array can not be always defined in advance. You can create a list and then convert that to array as below:
List<Integer> oddNumbers = new ArrayList<>();
for (int i = -6; i<38; i++){
if (i%2!=0){
oddNumbers.add(i);
}
}
Integer[] result = oddNumbers.toArray(new Integer[oddNumbers.size()]);
am trying to split elements in array list .
For example i have an arrayList like
List <String> elements = new ArrayList<String>();
elements // ["frnec","fdct","logic","mastro","seloger"]
The size of the elements should be dynamic...
List<List<String>> splittedlists = null;
i want to split the elements like ["frnec"] ,["fdct",logic"],["mastro", "seloger"].
splittedlists //[["frnec"] ,["fdct",logic"],["mastro", "seloger"]].
But the size of the new splittedlists should not exceed 4 ,based on that i have to chop the elements
i had got lots of code code to split lists.But i dont know how to set the maximum size of the 'splittedlists'.
but it will split by setting the target size of spitted elements
public static <T extends Object> List<List<T>> split(List<T> list, int targetSize) {
List<List<T>> lists = new ArrayList<List<T>>();
for (int i = 0; i < list.size(); i += targetSize) {
lists.add(list.subList(i, Math.min(i + targetSize, list.size())));
}
return lists;
}
My requirmeent is to split elements in the list by setting the maximum array size(here 4) of splittedlists
If elements are // ["frnec","fdct"] i want to split is as ["frnec"], ["fdct"]
if elements are // [0, 1, 2, 3, 4, 8, 6] i have to split without exceeding the new array size 4 like
[[0,1],[2,3],[4,8],[6]]
Here is my answer.. Guess you can understand what I'm doing..
List<String> list = new ArrayList<>();
for (int i = 1; i < 11; i++) {
list.add(i+"");
}
List<List<String>> lists = new ArrayList<>();
int x = list.size()/4;
int y = list.size()%4;
int j = 0;
for (int i = 0; i < list.size(); i=j) {
j = i+x;
if(y>0){
j++;
y--;
}
lists.add(list.subList(i, j));
}
System.out.println(list);
System.out.println(lists);
If you don't need to maintain the order you can use modulus (%) operator. So for 3 sublists, it would put the 0th element in the 0th sublist, the first element in the first sublist, the second element in the second sublist, the third element in the 0th sublist, the fourth element in the 1st sublist.... etc.
Something like:
for(int i = 0; i < list.size() ; i++){
listOfLists.get(i % 3).add(list.get(i));
}
Background:
I have an N-length array of positive random numbers that are certain to contain duplicates.
e.g. 10,4,5,7,10,9,10,9,8,10,5
Edit: N is likely to be 32, or some other power of two about that size.
The Problem:
I am trying to find the fastest way to replace the duplicates with the missing numbers from 0-(N-1). Using the above example, I want a result that looks like this:
10,4,5,7,0,9,1,2,8,3,6
The goal being to have one of each number from 0 to N-1, without just replacing all the numbers with 0-(N-1) (the random order is important).
Edit: It's also important that this replacement is deterministic, i.e. the same input will have the same output (not random).
My solution:
Currently implemented in Java, uses 2 boolean arrays to keep track of used/unused numbers (unique numbers/missing numbers in the range [0,N) ), and has an approximate worst-case runtime of N+N*sqrt(N).
The code follows:
public byte[] uniqueify(byte[] input)
{
boolean[] usedNumbers = new boolean[N];
boolean[] unusedIndices = new boolean[N];
byte[] result = new byte[N];
for(int i = 0; i < N; i++) // first pass through
{
int newIdx = (input[i] + 128) % N; // first make positive
if(!usedNumbers[newIdx]) // if this number has not been used
{
usedNumbers[newIdx] = true; // mark as used
result[i] = newIdx; // save it in the result
}
else // if the number is used
{
unusedIndices[i] = true; // add it to the list of duplicates
}
}
// handle all the duplicates
for(int idx = 0; idx < N; idx++) // iterate through all numbers
{
if(unusedIndices[idx]) // if unused
for(int i = 0; i < N; i++) // go through all numbers again
{
if(!usedNumbers[i]) // if this number is still unused
{
usedNumbers[i] = true; // mark as used
result[i] = idx;
break;
}
}
}
return result;
}
This seems like the fastest I can hope for, but I thought I'd ask the internet, because there are people much more clever than I who may have a better solution.
N.B. Suggestions/solutions do not have to be in Java.
Thank you.
Edit: I forgot to mention that I am converting this to C++. I posted my java implementation because it's more complete.
Use a balanced binary search tree to keep track of used/unused numbers instead of a boolean array. Then you're running time will be n log n.
The most straightforward solution would be this:
Go through the list and build the "unused" BST
Go through the list a second time, keeping track of numbers seen so far in a "used" BST
If a duplicate is found, replace it with a random element of the "unused" BST.
Here is how I would write it.
public static int[] uniqueify(int... input) {
Set<Integer> unused = new HashSet<>();
for (int j = 0; j < input.length; j++) unused.add(j);
for (int i : input) unused.remove(i);
Iterator<Integer> iter = unused.iterator();
Set<Integer> unique = new LinkedHashSet<>();
for (int i : input)
if (!unique.add(i))
unique.add(iter.next());
int[] result = new int[input.length];
int k = 0;
for (int i : unique) result[k++] = i;
return result;
}
public static void main(String... args) {
System.out.println(Arrays.toString(uniqueify(10, 4, 5, 7, 10, 9, 10, 9, 8, 10, 5)));
}
prints
[10, 4, 5, 7, 0, 9, 1, 2, 8, 3, 6]
The fastest way to do this is probably the most straightforward one. I would take a pass through the list of data keeping a count of each distinct value and marking where duplicates appeared. Then it is just a matter of forming a list of unused values and applying them in turn at the places where duplicates were found.
Tempting as it may be to use a C++ List, if speed is of the essence a simple C array is the most efficient.
This program show the principle.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int data[] = { 10, 4, 5, 7, 10, 9, 10, 9, 8, 10, 5 };
int N = sizeof(data) / sizeof(data[0]);
int tally[N];
memset(tally, 0, sizeof(tally));
int dup_indices[N];
int ndups = 0;
// Build a count of each value and a list of indices of duplicate data
for (int i = 0; i < N; i++) {
if (tally[data[i]]++) {
dup_indices[ndups++] = i;
}
}
// Replace each duplicate with the next value having a zero count
int t = 0;
for (int i = 0; i < ndups; i++) {
while (tally[t]) t++;
data[dup_indices[i]] = t++;
}
for (int i = 0; i < N; i++) {
cout << data[i] << " ";
}
return 0;
}
output
10 4 5 7 0 9 1 2 8 3 6
My approach would be
1. copy the array to a Set in Java.
Set will automatically remove duplicates in the fastest complexity possible(because Sun Micro has implemented it, generally their approach is the fastest like.. use of TimSort for sorting etc...)
Calculate size() of the set.
the size will give you no of duplicates present.
now copy array 0-n-1 to the same set... the missing values will get inserted.
I think it is even possible with running time n. The idea is to keep track of items used in the original list and additional items used during processing in a separate array. A possible java implementation looks like this:
int[] list = { 10, 4, 5, 7, 10, 9, 10, 9, 8, 10, 5 };
boolean[] used = new boolean[list.length];
for (int i : list) {
used[i] = true;
}
boolean[] done = new boolean[list.length];
int nextUnused = 0;
Arrays.fill(done, false);
for (int idx = 0; idx < list.length; idx++) {
if (done[list[idx]]) {
list[idx] = nextUnused;
}
done[list[idx]] = true;
while (nextUnused < list.length && (done[nextUnused] || used[nextUnused])) {
nextUnused++;
}
}
System.out.println(Arrays.toString(list));
List<Integer> needsReplaced = newLinkedList<Integer>();
boolean[] seen = new boolean[input.length];
for (int i = 0; i < input.length; ++i) {
if (seen[input[i]]) {
needsReplaced.add(i);
} else {
seen[input[i]] = true;
}
}
int replaceWith = 0;
for (int i : needsReplaced) {
while (seen[replaceWith]) {
++replaceWith;
}
input[i] = replaceWith++;
}
This should behave in about 2n. The list operations are constant time, and even though that second loop looks nested, the outer loop runs significantly less than n iterations, and the inner loop will only run a total of n times.
C# but should be easy to convert to java. O(n).
int[] list = { 0, 0, 6, 0, 5, 0, 4, 0, 1, 2, 3 };
int N = list.length;
boolean[] InList = new boolean[N];
boolean[] Used = new boolean[N];
int[] Unused = new int[N];
for (int i = 0; i < N; i++) InList[list[i]] = true;
for (int i = 0, j = 0; i < N; i++)
if (InList[i] == false)
Unused[j++] = i;
int UnusedIndex = 0;
for (int i = 0; i < N; i++)
{
if (Used[list[i]] == true)
list[i] = Unused[UnusedIndex++];
Used[list[i]] = true;
}
Edit: tried to convert it to java from c#. I don't have java here so it may not compile but should be easy to fix. The arrays may need to be initialized to false if java doesn't do that automatically.