Javascript Odd and Even Separation in an Array of Numbers

javascript odd and even separation in an array of numbers

Because you move every found even value to the end of the array:

0: 1 2 3 4 5 6 7 8 9
^-------------v
1: 1 3 4 5 6 7 8 9 2 3 is not checked, because of the incremented index after splicing
^-----------v
2: 1 3 5 6 7 8 9 2 4 5 is not checked
^---------v
3: 1 3 5 7 8 9 2 4 6 7 is not checked
^-------v
4: 1 3 5 7 9 2 4 6 8 9 is not checked
^-----v
5: 1 3 5 7 9 4 6 8 2
^---v
6: 1 3 5 7 9 4 8 2 6
^-v
7: 1 3 5 7 9 4 8 6 2
|
8: 1 3 5 7 9 4 8 6 2

But, you do not check the value after the found even value, like the value 3 in line zero, because it is never checked and stays at this place, as well as other actually uneven values. You could try the whole again with all even at the beginning of the array, like

var array = [2, 4, 6, 8, 1, 3, 5, 7, 9];    for (var i = 0; i < array.length; i++) {    if (array[i] % 2 === 0) {        array.push(array.splice(i, 1)[0]);        console.log(i + ': ' + array.join(' '));    }}
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

split an array into two arrays based on odd/even position

You could try:

var Arr1 = [1,1,2,2,3,8,4,6],
Arr2 = [],
Arr3 = [];

for (var i=0;i<Arr1.length;i++){
if ((i+2)%2==0) {
Arr3.push(Arr1[i]);
}
else {
Arr2.push(Arr1[i]);
}
}

console.log(Arr2);

JS Fiddle demo.

C Array Split By Odd Even Indexes Gives Garbage Values

Any access to a[i] or b[i] will be out of bounds when i is greater than 2. You should be using the aa and bb indices when filling the a[] and b[] arrays.

Also, the test for even or odd indices can be simplified since 0 is even.

Here is the fixed version of the array splitting loop:

    for (i = 0; i < n; i++)
{
if (i % 2 == 0)
{
// even
a[aa] = arr[i];

// For debug purpose
printf("a[%d]=%d, arr[%d]=%d, aa=%d\n", aa, a[aa], i, arr[i], aa);

aa++;
}
else
{
// odd
b[bb] = arr[i];

// For debug purpose
printf("b[%d]=%d, arr[%d]=%d, bb=%d\n", bb, b[bb], i, arr[i], bb);

bb++;
}
}

Separate odd even from string

You can try this simple solution, without using any other concepts like regex. For this, you can split your string and store it in a string array, and than iterating over an array you can check whether the number is odd or even. Following code will store all the even and odd numbers from your string into the array named even and odd.

String s = "1,2,3,4,5,6,7,8,9,10";

int even[] = new int[10];
int odd[] = new int[10];
String ar[] = s.split(",");
int j=0,k=0,oddChecker=0,evenChecker=0;
for(int i=0;i<ar.length;i++){
if(Integer.parseInt(ar[i])%2 == 0){
even[j] = Integer.parseInt(ar[i]);
++j;
evenChecker = 1;

}
else{
odd[k] = Integer.parseInt(ar[i]);
++k;
oddChecker = 1;
}

}


if(oddChecker == 0){
System.out.println("even");
System.exit(0);
}

if(evenChecker == 0){
System.out.println("odd");
System.exit(0);
}



System.out.println("Even numbers:");

for(int i=0;i<j;i++){
if(i!=j-1){
System.out.print(even[i]+",");
}
else{
System.out.print(even[i]);
}
}
System.out.println();
System.out.println("Odd numbers:");

for(int i=0;i<k;i++){
if(i!=k-1){
System.out.print(odd[i]+",");
}
else{
System.out.print(odd[i]);
}
}

Output:

Even numbers:

2,4,6,8,10

Odd numbers:

1,3,5,7,9

Don't forget to convert String to Integer when checking the condition and adding numbers to arrays. For that I've used Integer.parseInt(your_string).

How to sort elements in an array into order and even numbers and they should be in ascending order

Sorting on 'a' having a smaller modulus 2 than 'b'

OR 'a' is bigger than 'b'.

function sortArrayUnevenFirst (array) {    return array.sort(function(a,b){return b%2-a%2 || a-b});}console.log (sortArrayUnevenFirst([12,10,2,1,4,9,3,3]));console.log (sortArrayUnevenFirst(['12','10','2','1','4','9','3','3']));

How can I pick out the odd numbers and even numbers from a given array and then store them in another array?

You can do all that in one loop - that would be way faster. To know the correct position to put the number in, add extra counter for each array.

Your kind of approach

int[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
int[] odd = new int[10];
int[] even = new int[10];
int oddPos = 0;
int evenPos = 0;
for (int i = 0; i < num.length; i++) {
if (num[i] % 2 == 0) {
even[evenPos] = num[i];
evenPos++;
} else {
odd[oddPos] = num[i];
oddPos++;
}
}

However this would not be the best solution as you (in most cases) cannot determine the length of odd and even arrays beforehand. Then you should use either arraylists or count the values of each or something else.

More dynamic approach

As stated before - you need to determine the size of the arrays at first

int[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
int oddCount = 0, evenCount = 0;
int oddPos = 0, evenPos = 0;
//get the count of each type
for (int i = 0; i < num.length; i++) {
if (num[i] % 2 == 0)
oddCount++;
else
evenCount++;
}
//define arrays in correct sizes
int[] odd = new int[oddCount];
int[] even = new int[evenCount];
//put values in arrays
for (int i = 0; i < num.length; i++) {
if (num[i] % 2 == 0) {
even[evenPos] = num[i];
evenPos++;
} else {
odd[oddPos] = num[i];
oddPos++;
}
}

Separating even and odd numbers in different arrays?

There's a few issues going on here, but to get over exception you're asking about, you need to initialize your evens and odds arrays with a size (currently zero). Since they could potentially be size 10, try that first -

 int[] odds = new int[10];
int[] evens = new int[10];

Another hint - after that you are assigning the index of the number to the value in evens and odds. It looks like you want to assign the user inputted value instead - evens[x] = nums[i];

There's definitely more optimizations you can make, but it sounds like you may be required to use arrays here, so I don't want to start guessing at the rules of the assignment.



Related Topics



Leave a reply



Submit