How to shift array elements after removing one? - java

I need to add a shift function only with loops (it means I can't use any ListArray method et simila, so this is not a copy of any other question with those methods.) to this method which removes a specific integer from a given array, and put the outcome into another array, and then returning it.
public static int[] removeInt(int v, int[] in) {
int length = 0;
length = in.length;
int[] toReturn = new int[length];
for (int b = 0; b < length; b++) {
toReturn[b] = 0;
}
for (int i = 0; i < length; i++) {
if (in[i] != v) {
toReturn[i] = in[i];
}
}
return toReturn;
}
If the input for the array is {1, 2, 3, 4, 5}, and the number to remove is {2}, the output will be {1 0 3 4 5}, but it needs to be {1 3 4 5}, and I can't find a way to shift numbers to the left.
'v' comes from the main, user input. Same for 'in', fullfilled before by the user.

You will have to iterate over the array two times: The first time, you count how often v is contained in in. If count is 0, you can return in, otherwise you would have to create a new array of length in.length - count. Then again you have to iterate over in and add every value that is not v to your Array.
int count = 0;
for(int i: in) {
if (i==v) {
count++;
}
}
int[] result = new int[in.length-count];
int pos=0;
for(int i: in) {
if (i!=v) {
result[pos] = i;
pos++;
}
}
But you could use Collections to make it easier:
public static void main(String[] args) {
int v = 2;
int[] in = new int[]{1, 2, 3, 4, 5};
List<Integer> list = new ArrayList<>();
for(int i: in) {
if (i!=v) {
list.add(i);
}
}
Integer[] result = list.toArray(new Integer[]{});
}
The disadvantage of this, however, would be that you have an Array of Integer and not of int.

Have you considered using a LinkedList? This internally manages the array so you don't need to deal with moving the elements yourself; you can just call remove().

Related

How to add 1 to each value in an array (in Java)?

Here's the prompt:
Loop through an integer array adding one to each element. Return the resulting array. The array may be of any length including 0.
Here's what I have so far:
public int[] add1toEach(int[] nums) {
int[] array = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
nums[i]++;
}
return array;
}
It works with add1toEach[] and add1toEach[-1] but that's it. How do I get it to spit out [7, 7, 3] if add1toEach[6, 6, 2] is the input?
The fact is that you aren't assigning the new added result to the array, nor are you adding it properly.
The proper code is:
public static int[] add1toEach(int[] nums) {
int[] array = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
array[i]=nums[i]+1;
}
return array;
}
What's happening here is that we add 1 to the item, then we assign the item to an identical array, but which the items are all empty. This happens to every single item of the array, as demonstrated in the statement in the for loop i < nums.length; i++.
This working example:
public class MyClass {
public static void main(String args[]) {
int[] nums = {1, 2, 3};
int[] newnums = add1toEach(nums);
for(int i = 0; i < newnums.length; i++){
System.out.println(newnums[i]);
}
}
public static int[] add1toEach(int[] nums) {
int[] array = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
array[i]=nums[i]+1;
}
return array;
}
}
Produces
2
3
4
What you're code doing is it only returns the untouched int[] array.
Since you have set the length of your array, you just need to set values to your array with position array[i] to make the values assigned and intact position.
array[i]=nums[i]++;

Expanding an array to represent an index as many times as the magnitude of original value

Example: expand(new int[]{3, 2, 5}) -> {0, 0, 0, 1, 1, 2, 2, 2, 2, 2}
I am trying to have it make a a new array to print the index of say 3, 3 times. So 3 would be 0,0,0.
public static int[] expand(int[] input) {
int c = 0;
int[] myArray = new int[sum(input)];
if(input.length == 0){
return new int[0];
}
for(int i = 0; i < input.length; i++) {
int a = input[i];
for(int j = c; j < a; j++) {
c += j;
myArray[j] = i;
}
}
return myArray;
}
Currently this only partially works and I cant seem to figure out how to go through the full array properly. In addition, index zero seems to get skipped.
You were close! It seems that you just need to modify your nested for-loop slightly:
for (int j = 0; j < a; j++) {
myArray[c++] = i;
}
Seeing as you're using c to keep track of the current index, this simply sets the element at index c to i and increments c. You can also remove a and use input[i] in place of it.
Note: It is easier to start j from 0 rather than from c.
Functional method
public class Main {
public static void main(final String... args) {
int[] items = expand(new int[]{3, 2, 5});
System.out.println(Arrays.toString(items));
}
public static int[] expand(int[] input) {
return IntStream.range(0, input.length)
.flatMap(p -> IntStream.generate(() -> p).limit(input[p]))
.toArray();
}
}
This makes a stream of the index, and for each item takes that many of the index, putting all of them into an array

Flatten 2d or 3d Array in Java

I need to take a multidimensional array and flatten it so that if the array looks like:
[1,2,3]
[4,5,6]
Then it is transformed to:
[1,2,3,4,5,6]
I also need to accommodate 3D arrays and flatten them also.
Here is what I've got so far...
class ArrayOperations {
private int[] postFlattenedArray;
public ArrayOperations {
flattenedArray = new int[arrayDimensions[0]];
// arrayDimesnsions of 3,4 is a a 3x4 array; 3,4,5 is a 3x4x5 array
//set up ints to hold total size and dimensionality of array
int prod = 1
int dimensions = 0;
newArray = new int[prod - 1];
for (int i: arrayDimensions) {
prod = prod * i;
dimensions += 1;
}
int k = 0;
for ( int i: arrayDimensions ){
for ( int j = 0; j <= i; j++ ){
newArray[k++] = flattenedArray[j][0][0]
}
}
}
I know I need to run some nested for loops, but I'm having difficulty grasping how I would do this for different array dimensions. So if the array has dimensions; 3,5,6 then I am not sure how to run a loop so that it iterates like:
[1..4][0][0], [0][1..5][0], [0][0][1..6]
It seems to that the simplest solution is implementing a recursive function flatten (which will probably have to be generic to be able to handle arrays of anything), which will return the array itself if already flat or recursively call itself on all the internal arrays and concatenate the results.
Try this.
static void flatten(Object object, List<Integer> list) {
if (object.getClass().isArray())
for (int i = 0; i < Array.getLength(object); ++i)
flatten(Array.get(object, i), list);
else
list.add((int)object);
}
static int[] flatten(Object object) {
List<Integer> list = new ArrayList<>();
flatten(object, list);
int size = list.size();
int[] result = new int[size];
for (int i = 0; i < size; ++i)
result[i] = list.get(i);
return result;
}
and
int[][][] array = {
{{1, 2, 3}, {4, 5}},
{{6}, {7, 8, 9}},
};
System.out.println(Arrays.toString(flatten(array)));
// -> [1, 2, 3, 4, 5, 6, 7, 8, 9]

Method that creates new array only from the even numbers in Java

I am trying to create method that creates new array only from the even numbers from another array. However, I do not understand why I have the right length (only with the number of the even numbers) of my output array, but I have 0s instead of the actual numbers.
I know that the problem is simple, but right now I am stuck with it.
import java.util.*;
public class test{
public static int [] myMethod(int []arr){
int [] temp;
int howManyEven = 0;
for(int i=0;i<arr.length;i++){
if(arr[i]%2==0){
howManyEven++;
}
}
temp = new int [howManyEven];
int evenNum = 0;
for(int i=0;i<temp.length;i++){
boolean even = false;
for(int j=0;j<arr.length;j++){
if(arr[j]%2==0){
even = true;
evenNum = arr[j];
}
}
if(!even){
temp[i]=evenNum;
}
}
return temp;
}
public static void main (String[]args){
int [] myArray = {5,5,8,9,7,4,4,2,3};
System.out.println(Arrays.toString(myMethod(myArray)));
}
}
In the second part of your myMethod, where there is nested for loop, your outer loop should iterate over arr. It can be simplified to below
temp = new int [howManyEven];
int j = 0;
for(int i=0;i<arr.length;i++){
if(arr[i]%2 == 0) {
temp[j++] = arr[i];
}
}
Better you can use java collections and finish the work in single loop
public static Integer[] myMethod(int []arr){ //notice the return type is Integer[] instead of int[]
List<Integer> evens = new ArrayList<Integer>();
for(int a : arr) {
if(a%2 == 0) {
evens.add(a);
}
}
return evens.toArray(new Integer[evens.size()]);
}
Look again at this:
if(!even){
temp[i]=evenNum;
}
I think you will notice yourself what's wrong.
Instead of processing the information twice for even numbers, you can create a List of integers which are 'even' and return that, or convert into primitive int[] and return that.
public static int[] myMethod(int[] arr) {
List<Integer> even = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
if(arr[i]%2==0){
even.add(arr[i]);
}
}
int []result = new int[even.toArray().length];
Iterator it = even.iterator();
int count=0;
while(it.hasNext()){
result[count++]=(int)it.next();
}
return result;
}
public static void main(String[] args) {
int[] myArray = {5, 5, 8, 9, 7, 4, 4, 2, 3};
System.out.println(Arrays.toString(myMethod(myArray)));
}
output
run:
[8, 4, 4, 2]
BUILD SUCCESSFUL (total time: 0 seconds)
The easiest way would be myMethod should create a temparory list which collects the even numbers from the input array and then convert it to an output array containing only the even elements which should be returned to the caller
Working Example
public static void main(String... str) {
// results array has only even numbers
Integer[] results = myMethod(new int[]{5, 5, 8, 9, 7, 4, 4, 2, 3});
for(int i=0;i<results.length;i++)
System.out.println(results[i]); // print each even number
}
static Integer[] myMethod(int[] inputArray) {
List<Integer> temp = new ArrayList<Integer>();
for (int i = 0; i < inputArray.length; i++) {
if (inputArray[i] % 2 == 0) {
temp.add(inputArray[i]);
}
}
return temp.toArray(new Integer[5]);
}
Hope this code clarifies you well.

How to remove duplicates from a list using an auxiliary array in Java?

I am trying to remove duplicates from a list by creating a temporary array that stores the indices of where the duplicates are, and then copies off the original array into another temporary array while comparing the indices to the indices I have stored in my first temporary array.
public void removeDuplicates()
{
double tempa [] = new double [items.length];
int counter = 0;
for ( int i = 0; i< numItems ; i++)
{
for(int j = i + 1; j < numItems; j++)
{
if(items[i] ==items[j])
{
tempa[counter] = j;
counter++;
}
}
}
double tempb [] = new double [ items.length];
int counter2 = 0;
int j =0;
for(int i = 0; i < numItems; i++)
{
if(i != tempa[j])
{
tempb[counter2] = items[i];
counter2++;
}
else
{
j++;
}
}
items = tempb;
numItems = counter2;
}
and while the logic seems right, my compiler is giving me an arrayindexoutofbounds error at
tempa[counter] = j;
I don't understand how counter could grow to above the value of items.length, where is the logic flaw?
You are making things quite difficult for yourself. Let Java do the heavy lifting for you. For example LinkedHashSet gives you uniqueness and retains insertion order. It will also be more efficient than comparing every value with every other value.
double [] input = {1,2,3,3,4,4};
Set<Double> tmp = new LinkedHashSet<Double>();
for (Double each : input) {
tmp.add(each);
}
double [] output = new double[tmp.size()];
int i = 0;
for (Double each : tmp) {
output[i++] = each;
}
System.out.println(Arrays.toString(output));
Done for int arrays, but easily coud be converted to double.
1) If you do not care about initial array elements order:
private static int[] withoutDuplicates(int[] a) {
Arrays.sort(a);
int hi = a.length - 1;
int[] result = new int[a.length];
int j = 0;
for (int i = 0; i < hi; i++) {
if (a[i] == a[i+1]) {
continue;
}
result[j] = a[i];
j++;
}
result[j++] = a[hi];
return Arrays.copyOf(result, j);
}
2) if you care about initial array elements order:
private static int[] withoutDuplicates2(int[] a) {
HashSet<Integer> keys = new HashSet<Integer>();
int[] result = new int[a.length];
int j = 0;
for (int i = 0 ; i < a.length; i++) {
if (keys.add(a[i])) {
result[j] = a[i];
j++;
}
}
return Arrays.copyOf(result, j);
}
3) If you do not care about initial array elements order:
private static Object[] withoutDuplicates3(int[] a) {
HashSet<Integer> keys = new HashSet<Integer>();
for (int value : a) {
keys.add(value);
}
return keys.toArray();
}
Imagine this was your input data:
Index: 0, 1, 2, 3, 4, 5, 6, 7, 8
Value: 1, 2, 3, 3, 3, 3, 3, 3, 3
Then according to your algorithm, tempa would need to be:
Index: 0, 1, 2, 3, 4, 5, 6, 7, 8, ....Exception!!!
Value: 3, 4, 5, 6, 7, 8, 4, 5, 6, 7, 8, 5, 6, 7, 8, 6, 7, 8, 7, 8, 8
Why do you have this problem? Because the first set of nested for loops does nothing to prevent you from trying to insert duplicates of the duplicate array indices!
What is the best solution?
Use a Set!
Sets guarantee that there are no duplicate entries in them. If you create a new Set and then add all of your array items to it, the Set will prune the duplicates. Then it is just a matter of going back from the Set to an array.
Alternatively, here is a very C-way of doing the same thing:
//duplicates will be a truth table indicating which indices are duplicates.
//initially all values are set to false
boolean duplicates[] = new boolean[items.length];
for ( int i = 0; i< numItems ; i++) {
if (!duplicates[i]) { //if i is not a known duplicate
for(int j = i + 1; j < numItems; j++) {
if(items[i] ==items[j]) {
duplicates[j] = true; //mark j as a known duplicate
}
}
}
}
I leave it to you to figure out how to finish.
import java.util.HashSet;
import sun.security.util.Length;
public class arrayduplication {
public static void main(String[] args) {
int arr[]={1,5,1,2,5,2,10};
TreeSet< Integer>set=new TreeSet<Integer>();
for(int i=0;i<arr.length;i++){
set.add(Integer.valueOf(arr[i]));
}
System.out.println(set);
}
}
You have already used num_items to bound your loop. Use that variable to set your array size for tempa also.
double tempa [] = new double [num_items];
Instead of doing it in array, you can simply use java.util.Set.
Here an example:
public static void main(String[] args)
{
Double[] values = new Double[]{ 1.0, 2.0, 2.0, 2.0, 3.0, 10.0, 10.0 };
Set<Double> singleValues = new HashSet<Double>();
for (Double value : values)
{
singleValues.add(value);
}
System.out.println("singleValues: "+singleValues);
// now convert it into double array
Double[] dValues = singleValues.toArray(new Double[]{});
}
Here's another alternative without the use of sets, only primitive types:
public static double [] removeDuplicates(double arr[]) {
double [] tempa = new double[arr.length];
int uniqueCount = 0;
for (int i=0;i<arr.length;i++) {
boolean unique = true;
for (int j=0;j<uniqueCount && unique;j++) {
if (arr[i] == tempa[j]) {
unique = false;
}
}
if (unique) {
tempa[uniqueCount++] = arr[i];
}
}
return Arrays.copyOf(tempa, uniqueCount);
}
It does require a temporary array of double objects on the way towards getting your actual result.
You can use a set for removing multiples.

Categories

Resources