Reverse numbers in function without using ".reverse" method in Javascript - javascript

function revertNumbers(...numberArray) {
let rev = [];
for(let i = 0; i <numberArray.length; i++)
{
rev.push(numberArray[i])
}
return rev.reverse();
}
console.log("revertNumbers", revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) === "9,8,7,6,5,4,3,2,1,0");
Can you please show me the how to reverse number in this code that the statement will be true? Also without using .reverse method. Is it possible to make it in another for loop by just changing this statement:
(let i = 0; i <numberArray.length; i++)

You just need to reverse the direction of your loop. Means start i with last index and then gradually decrease it to 0
function revertNumbers(...numberArray) {
let rev = [];
for(let i = numberArray.length - 1; i >= 0; i--)
{
rev.push(numberArray[i])
}
return rev.join(",")
}
console.log("revertNumbers", revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) === "9,8,7,6,5,4,3,2,1,0");
This is also a good use case of reduceRight()
const revertNumbers = (...arr) => arr.reduceRight((ac, a) => ([...ac, a]), []).join(',')
console.log("revertNumbers", revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) === "9,8,7,6,5,4,3,2,1,0");

Here is a solution that manipulates the array in place, and only has to traverse half of the original array in order to reverse it. This ekes out some modest performance gains compared to other answers in this thread (my function narrowly beats out or matches the speed of even the native reverse method in ops/sec), but micro-optimizations are largely irrelevant for this problem unless you are talking about a truly massive list of numbers.
Nonetheless, here is my answer:
const revNums = (...numArray) => {
for (
let arrLen = numArray.length,
breakPoint = ((arrLen / 2)|0) - 1,
i = arrLen,
k = 0,
temp;
--i !== breakPoint;
++k
) {
temp = numArray[i];
numArray[i] = numArray[k];
numArray[k] = temp;
}
return numArray.join(',');
};

You could reduce the original array and unshift each element onto the new array.
function revertNumbers(...numberArray) {
return numberArray.reduce((r, e) => { r.unshift(e); return r }, []).join(',')
}
console.log("revertNumbers", revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) === "9,8,7,6,5,4,3,2,1,0")
If you don't want to unshift, you can concat in reverse.
function revertNumbers(...numberArray) {
return numberArray.reduce((r, e, i, a) => r.concat(a[a.length - i - 1]), []).join(',')
}
console.log("revertNumbers", revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) === "9,8,7,6,5,4,3,2,1,0")

You could take a value and the rest of the arguments and use a recursive approach to get a reversed array of arguments.
function revertNumbers(v, ...rest) {
return rest.length
? [...revertNumbers(...rest), v]
: [v];
}
console.log(revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));

It is to ask how many solutions can be possible ?
here are 3 of them...
"use strict";
const targetString = '9,8,7,6,5,4,3,2,1,0'
;
function soluce_1(...numberArray)
{
const rev = [];
for( let i=numberArray.length;i--;) { rev.push(numberArray[i]) }
return rev.join(',')
}
function soluce_2(...numberArray)
{
const rev = [];
let pos = numberArray.length;
for(let N in numberArray) { rev[--pos] = N }
return rev.join(",")
}
function soluce_3(...numberArray)
{
const rev = [];
while(numberArray.length) { rev.push(numberArray.pop()) }
return rev.join(',')
}
console.log('soluce_1 ->', (soluce_1(0,1,2,3,4,5,6,7,8,9)===targetString) );
console.log('soluce_2 ->', (soluce_2(0,1,2,3,4,5,6,7,8,9)===targetString) );
console.log('soluce_3 ->', (soluce_3(0,1,2,3,4,5,6,7,8,9)===targetString) );
And yes, I code in Whitesmiths style, please respect this (the reason for the downVote for correct answers ?)
https://en.wikipedia.org/wiki/Indentation_style#Whitesmiths_style

You might reverse like this:
function reverse(...a) {
const h = a.length >> 1, l = a.length-1;
for (let i = 0; i < h; ++i) [a[i], a[l-i]] = [a[l-i], a[i]];
return a;
}
console.log(reverse(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).join(','));

Related

What is the efficient way to find an array inside an array?

Find an Array of Values Inside an Array
Lets say I have an array [1,2,3,8,2,3,4,5,6,7,8] and I want to find the first occurrence of the values [3,4,5,6] together, how might I do that? I can use Array.prototype.findIndex, but when I am looking for a large amount of values in a large array, it doesn't feel like the proper way to do it.
What fails:
let largeArray = [1,2,3,8,2,3,4,5,6,7,8];
let smallArray = [3,4,5,6];
//Problem: an array isn't a function
largeArray.findIndex(smallArray);
/*
Problem: always returns -1 because it checks each element
rather than looking for a group of elements.
*/
largeArray.indexOf(smallArray);
//Problem: same as using indexOf
largeArray.findIndex(item=>smallArray);
What works:
let largeArray = [1,2,3,8,2,3,4,5,6,7,8];
let smallArray = [3,4,5,6];
//Here is what works, but isn't ideal
largeArray.findIndex((item, index, arr) => {
let isTheOne = item == smallArray[0] &&
arr[index + 1] == smallArray[1] &&
arr[index + 2] == smallArray[2] &&
arr[index + 3] == smallArray[3];
return isTheOne;
});
//It returns 5, which is correct.
To Be Continued
I am currently using what works, but what if largeArray had the length of a million and smallArray had the length of 300. That would be 1 line of item == smallArray[0] &&, 298 lines of arr[index + x] == smallArray[x] &&, and 1 line of arr[index + x] == smallArray[x];. I don't want to use Array.prototype.map, Array.prototype.filter, Array.prototype.forEach, a for loop, or a while loop. This is because Array.prototype.map, Array.prototype.forEach, and the loops take a very long time to complete. I don't want to use Array.prototype.filter because that doesn't give me the index.
You were on the right track, you just want to use every() to look over the small index to check that each index matches
const largeArray = [1, 2, 3, 8, 2, 3, 4, 5, 6, 7, 8];
let smallArray = [3, 4, 5, 6];
const index = largeArray.findIndex(
(item, index, arr) =>
smallArray.every(
(n, sIndex) => n === arr[index + sIndex]
)
);
console.log(index);
You could add a check beforehand to not have to go in every... not sure what that would improve.
const index = largeArray.findIndex(
(item, index, arr) =>
item === smallArray[0] &&
smallArray.every(
(n, sIndex) => n === arr[index + sIndex]
)
);
Other approach is using strings
const largeArray = [1, 2, 3, 8, 2, 3, 4, 5, 6, 7, 8];
const smallArray = [3, 4, 5, 6];
const largeStr = largeArray.join(",");
const smallStr = smallArray.join(",");
const strIndex = largeStr.indexOf(smallStr);
const index = strIndex > -1 ? largeStr.substr(0,strIndex-1).split(",").length : -1;
console.log(index)
To figure out what is better is really based on your use case.
You can use .join to convert the arrays to strings, and use .indexOf to get the index given that you will remove the additional commas:
const getIndexOfSubArray = (arr=[], sub=[]) => {
const str = arr.join();
const subStr = sub.join();
const index = str.indexOf(subStr);
return index < 0 ? -1 : str.substr(0, index-1).split(',').length;
}
console.log( getIndexOfSubArray([1,2,3,8,2,3,4,5,6,7,8], [3,4,5,6]) );
You could iterate by hand and check the items with indexOf.
function getIndex(array, subarray) {
let p = -1,
first = subarray[0];
while ((p = array.indexOf(first, p + 1)) !== -1) {
let i = p,
complete = true;
for (const s of subarray) {
if (s !== array[i++]) {
complete = false;
break;
}
}
if (complete) return p;
}
return -1;
}
console.log(getIndex([1, 2, 3, 8, 2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6])); // 5
Here is a simple approach to this problem:
let largeArray = [1, 2, 3, 8, 2, 3, 4, 5, 6, 7, 8];
let smallArray = [3, 4, 5, 6];
let s = 0,
i = 0,
j = 0;
let SLen = smallArray.length,
LLen = largeArray.length;
while (i < LLen && j < SLen && SLen - j <= LLen - i) {
if (j == 0) {
s = i;
}
if (largeArray[i] == smallArray[j]) {
j++;
} else {
j = 0;
i = s;
}
i++;
}
let index = i - j;
if (j == SLen) {
console.log(`found at index ${index}`);
} else {
console.log('not found');
}

Add every n items in an array

I have an array like so:
[5, 12, 43, 65, 34 ...]
Just a normal array of numbers.
What I wan't to do is write a function group(n, arr) which adds every n numbers in the array.
For example if I call group(2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) it should return
[
3 //1+2,
7 //3+4,
11 //5+6,
15 //7+8,
19 //9+10,
11 //whatever remains
]
I haven't tried anything yet, I will soon update with what I can.
You can use .reduce as follows:
function group(n, arr) {
// initialize array to be returned
let res = [];
// validate n
if(n > 0 && n <= arr.length) {
// iterate over arr while updating acc
res = arr.reduce((acc, num, index) => {
// if the current index has no remainder with n, add a new number
if(index%n === 0) acc.push(num);
// else update the last added number to the array
else acc[acc.length-1] += num;
// return acc in each iteration
return acc;
}, []);
}
return res;
}
console.log( group(2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) );
This approach features two loops, one for checking the outer index and anoter for iterating the wanted count of items for summing.
function group(n, array) {
const result = [];
let i = 0;
while (i < array.length) {
let sum = 0;
for (let j = 0; j < n && i + j < array.length; j++) {
sum += array[i + j];
}
result.push(sum);
i += n;
}
return result;
}
console.log(group(2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));
You could use Array.from to create the result array and then use its mapper to make the sums. These sums can be made by using reduce on the relevant slice of the array .
This is a functional programming solution:
const group = (step, arr) =>
Array.from({length: Math.ceil(arr.length/step)}, (_, i) =>
arr.slice(i*step, (i+1)*step).reduce((a, b) => a+b)
);
console.log(group(2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));

Time complexity of a de-duping algorithm

Here is a function for removing duplicates from an array.
function dedupe(arr) {
var seen = {};
arr.forEach((e,i)=>{
if (seen[e]) {
arr.splice(i, 1);
}
seen[e] = true;
});
return arr;
}
console.log(dedupe([1, 2, 1, 3, 4]));
I am interested in the time complexity of this function.
If we assume that Array is backed by a real array, does that the time complexity can be analysed as follows?
allocation of seen: O(1)
enumerate all elements: O(n)
removal of a duplicate: O(n) (because re-allocation required item by item?)
return O(1)
So is this an O(n^2) algorithm?
Edit:
Corrected for indexing issue.
function dedupe(arr) {
var seen = {};
for(let i = 0; i < arr.length; i++) {
const e = arr[i];
if (seen[e]) {
arr.splice(i, 1);
i--; // we have modified the array and need to continue from the current index
}
seen[e] = true;
}
return arr;
}
console.log(dedupe([1, 2, 1, 3, 1, 4, 4, 7, 6, 7, 7, 7, 1, 5]));
For those upset by the performance of the above, this is O(N) I think.
I wanted to de-dupe in-place. Use of Set maintains the order across host environments.
function dedupe(arr) {
var seen = new Set();
for(let i = 0; i < arr.length; i++) {
seen.add(arr[i]);
}
arr.length = 0; // empty the array
return arr.concat(...seen.keys());
}
console.log(dedupe([1, 2, 1, 3, 1, 4, 4, 7, 6, 7, 7, 7, 1, 5]));
One approach would be to use the Javascript Set. You could simply do this:
const removeDuplicates = array => (new Set(array)).values()
This will return an iterator, and not an array, however this can easily be fixed. Also, sets are not yet supported in most browsers. The complexity of this should be O(n).
Another approach more similar to yours (but probably identical to the Set, since I'm gonna guess it's implemented using the same underlying structure) would be like this:
const removeDuplicates = array =>
Object.keys(array.reduce((agg, x) => { agg[x] = true; return agg }, {}))
The time complexity of this should be O(m+n) where m will be the number of unique items, which will always be <= n, therefore O(n).
Also, the time complexity you worked out seems correct.
You could save seen by filtering by index:
var t1 = [1, 2, 1, 1, 3, 1, 1, 4];
function uniqueList(list) {
return list.filter(function (value, index, arr) {
return list.indexOf(value) == index;
});
}
console.log(t1);
console.log(uniqueList(t1));
My answer, builds a new array. Maybe is O(n).
function dedupe(arr) {
var result = [];
var seen = {};
for(let i = 0; i < arr.length; i++) {
const e = arr[i];
if (seen[e]) {
//skip
} else {
seen[e] = true;
result.push(e);
}
}
return result;
}
console.log(dedupe([1, 2, 1, 3, 1, 4, 4, 7, 6, 7, 7, 7, 1, 5]));

Counting the same nearest values in Javascript array

Given this array:
[1, 1, 2, 1, 1, 1, 2, 3, 4, 4, 4, 6, 4, 4]
How can I efficently count the nearest same elements in array, the result I would expect is:
1 => 2,
2 => 1,
1 => 3,
2 => 1,
3 => 1,
4 => 3,
6 => 1,
4 => 2
I don't know how to formulate correctly the question but I think the example is pretty clear.
I tried using reduce to make more compact and elegant but I always get a value with total number of same value in array.
let result = testArray.reduce((allValues, value) => {
if(value in allValues){
allValues[value]++;
} else {
allValues[value] = 1;
}
return allValues;
}, {});
You could check the last element and if equal, increment count, if not push a new object to the result set.
var array = [1, 1, 2, 1, 1, 1, 2, 3, 4, 4, 4, 6, 4, 4],
count = array.reduce((r, a, i, aa) => {
if (aa[i - 1] === a) {
r[r.length - 1].count++;
} else {
r.push({ value: a, count: 1 });
}
return r;
}, []);
console.log(count);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Another solution using Array.prototype.reduce and a hash table - see demo below:
var array = [1, 1, 2, 1, 1, 1, 2, 3, 4, 4, 4, 6, 4, 4];
var result = array.reduce(function(hash){
return function(p,c,i){
hash[c] = (hash[c] || 0) + 1;
if(hash.prev && (hash.prev !== c || i == array.length - 1)) {
let obj= {};
obj[hash.prev] = hash[hash.prev];
delete hash[hash.prev];
p.push(obj);
}
hash.prev = c;
return p;
}
}(Object.create(null)),[]);
console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}
While Array.prototype.reduce is a nice function, it does have some compatibility issues.
Here is a solution using old school for loops:
var arr = [1, 1, 2, 1, 1, 1, 2, 3, 4, 4, 4, 6, 4, 4];
var results = [];
for (var indexA = 0; indexA < arr.length; indexA++) {
var a = arr[indexA];
if (results.length > 0) {
if (a == results[results.length - 1]["value"]) {
continue;
}
}
var r = { value: a, index: indexA, count: 0 };
for (var indexB = indexA; indexB < arr.length; indexB++) {
var b = arr[indexB];
if (a != b) {
break;
}
r.count++;
}
results.push(r);
}
console.log(results);
The posted solutions are fine but as noted by #Emil S. Jørgensen there might be compatibility issues using Array.reduce. Also, the suggested old school solution by #Emil S. Jørgensen uses two loops. If you want to have a slightly more efficient,simple and straightforward solution which will work in all browsers then use:
var arr = [1, 1, 2, 1, 1, 1, 2, 3, 4, 4, 4, 6, 4, 4];
var result = [];
var current = arr[0];
var count = 1;
for (var i = 1; i <= arr.length; i++)
{
if(arr[i] === current)
{
count+= 1;
}
else
{
var newObj = {};
newObj[current] = count;
result.push(newObj);
current = arr[i];
count = 1;
}
}
console.log(result); //prints the solution

Best practice when sorting an array in pure javascript, if I have to send one group to the back

If I have the following array:
[0, 1, 3, 0, 4, 2]
And I'd like to sort it ascending order, barring zeros which I need on the end:
[1, 2, 3, 4, 0, 0]
Bear in mind I don't have access to underscore or linq.js for this solution.
My current solution works, but feels quite heavy, long, and not very elegant. Here's my code:
function sortNumbers(numbers) {
var zeroNumbers = [];
var notZeroNumbers = [];
for (var i = 0; i < numbers.length; i++) {
if (numbers[i] === 0) {
zeroNumbers.push(numbers[i]);
} else {
notZeroNumbers.push(numbers[i]);
}
}
var sortedNumbers = notZeroNumbers.sort(function (a, b) {
return parseFloat(a) - parseFloat(b);
});
for (var x = 0; x < zeroNumbers.length; x++) {
sortedNumbers.push(zeroNumbers[x]);
}
return sortedNumbers;
}
Can I improve on this solution?
This is not related to this question, but I was searching for "pure sort javascript" and this is the first answer.
Because sort mutates the original array, the best practice when sorting an array is to clone it first.
const sortedArray = [...array].sort(/* optional comparison function*/)
simply try
var output = [0, 1, 3, 0, 4, 2].sort(function(a, b) {
a = a || Number.MAX_SAFE_INTEGER; //if a == 0 then it will be a falsey value and a will be assigned Number.MAX_SAFE_INTEGER
b = b || Number.MAX_SAFE_INTEGER;
return a - b;
});
console.log(output)
var arr = [0, 1, 3, 0, 4, 2, 9, 8, 7, 0];
arr.sort(function (left, right) {
return left == right ? 0 : (left === 0 ? 1 : (left < right ? -1 : 1));
});
console.log(arr)
This will always put zeroes at the end regardless of the size of the number.
Another alternative solution using Array.sort, Array.splice and Array.push functions:
var arr = [0, 1, 3, 0, 4, 2];
arr.sort();
while(arr[0] === 0) { arr.splice(0,1); arr.push(0); }
console.log(arr); // [1, 2, 3, 4, 0, 0]
You can use sort for this, which takes a closure/callback.
var sortedArray = [0, 1, 3, 0, 4, 2].sort(function(currentValue, nextValue) {
if(currentValue === 0) {
return 1;
} else {
return currentValue - nextValue;
}
});

Categories

Resources