Related
what is the best algorithm to find two numbers in an array which their sum be a specific number ? for example Array = [1,5,73,68,2] and specific number is 70 . output must be 68 + 2 = 70
so if you want a better solution with better time complexity you can reach O(n) instead of O(n^2) and iterating the array only once.
but you need a data structure that has O(1) for finding data, something like hashmap.
and code would be like this:
function find(array, sum){
let answer = {}
for(const num of array){
if(answer[num]){
return { first: sum - num, second: num}
}
answer[sum - num] = true
}
return false
}
let arr = [ 2, 5, 7, 3, 82]
console.log(find( arr, 10))
I do my try, hope this helps.
function getPairs(arr, n, sum)
{ let temp = []
for (let i = 0; i < n; i++)
for (j = i + 1; j < n; j++)
if (arr[i] + arr[j] == sum)
temp.push([arr[i],arr[j]])
return temp
}
let arr = [1,2,3,4]
console.log(getPairs(arr, arr.length, 3))
You can use a for loop
sums=[]
function findinarray(specialnumb){
array = [1,5,73,68,2]
for(let i=0;i<array.length;i++){
for(let j=1+i;j<array.length;j++){
o=array[i]
b=array[j]
if(o+b==specialnumb)
sums.push(o,b)
}
}
return sums
}
console.log(findinarray(70))
function example(){
var numbersArray = ['1','5','73','68','2'];
var number = 70;
for (let i = 0; i < numbersArray.length; i++) {
for (let j = 0; j < numbersArray.length; j++) {
if(Number(numbersArray[i])+Number(numbersArray[j]) == number){
console.log('found this:'+numbersArray[i]+'+'+numbersArray[j] +'='+number)
}
}
}
}
example();
Ofcourse you dont have to use console.log , this is just an example.
A simple example , i'm pretty sure there are another ways to do it.
I hope this will help you
Here you go: Working solution. Just run snippet to see in action.
You can pass array() to this function and total number you want to count in that array. In return it will console.log the indexes totaling that number which you wanted.
function checkSum(arrayValues, total) {
var map = [];
var indexxxx = [];
for (var x = 0; x < arrayValues.length; x++) {
if (map[arrayValues[x]] != null) {
var index = map[arrayValues[x]];
indexxxx[0] = index;
indexxxx[1] = x;
break;
} else {
map[total - arrayValues[x]] = x;
}
}
console.log('Array Index at ' + indexxxx[0] + ' & ' + indexxxx[1] + ' sums equals to ' + total)
}
checkSum([1, 5, 73, 68, 2], 70)
If you want to know the exact value totalling the number then you do this:
function checkSum(arrayValues, total) {
var indexxxx = [];
for (var x = 0; x < arrayValues.length; x++) {
for (var j = 1 + x; j < arrayValues.length; j++) {
o = arrayValues[x]
b = arrayValues[j]
if (o + b == total)
indexxxx.push(o, b)
}
}
console.log('Array value at ' + indexxxx[0] + ' & ' + indexxxx[1] + ' sums equals to ' + total)
}
checkSum([1, 5, 73, 68, 2], 70)
I just created this utility in order to find the pairs whose sum will be equal to the provided/expected sum. Here I have not used nested loops. This will also eliminate duplicate pairs.
getPairs = (array, sum) => {
const obj = {}
const finalResult = []
array.forEach(d => {
obj[sum - d] = d
})
Object.keys(obj).forEach(key => {
if (obj[sum - key]) {
finalResult.push([parseInt(key), sum - key])
delete obj[key]
}
})
return finalResult
}
const array = [1, 5, 73, 68, 2, 4, 74]
const sum = 78
console.log(getPairs(array, sum))
Hope this helps.
I have data like this.
var abc =",,,,,,,,,,,,,,,paul,2000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
abc = abc.split(",");
let stub={};
var results=[];
var key=["name","value","acc"];
var i=0;
var j=0;
for( var i = 0 ; i <abc.length - 1;i++){
stub[key[j]=abc[i];
j++
if(j==3){
results.push(stub);
stub={};
j=0;
}
}
abc = results;
I would like to get those values arranges in form of array of object having those 3 keys:
output should be:
abc = [{"name": "paul", "value": "2000","acc":"12sc21"},{"name":"logan","value":"123","acc":"21sdf34"},{"name":"vfsarwe","value":"456456","acc":"32fd23"}];
but not able to get the desired output. this output only comes when string don't have ,,,,,, in starting. But the data i'm getting is sometimes having ,,,,, in stating.
You can use abc.replace(/(^[,\s]+)/g, '') to remove leading commas or whitespace from the String. Your for loop is also not running for long enough; it is looping until there is only one element left in the Array and then stopping.
Change
for(var i = 0 ; i < abc.length-1; i++)
To
for(var i = 0 ; i < abc.length; i++)
var abc =",,,,,,,,,,,,,,,paul,2000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
abc = abc.replace(/(^[,\s]+)|([,\s]+$)/g, '').split(",");
let stub={};
var results=[];
var key=["name","value","acc"];
var i=0;
var j=0;
for(var i = 0 ; i < abc.length; i++){
stub[key[j]]=abc[i];
j++
if(j==3){
results.push(stub);
stub={};
j=0;
}
}
abc = results;
console.log(abc);
You can use .replace(/^\,+/, '') to remove all leading commas, then split by comma to get an array, then loop over this array using 3 as step and construct your results:
var abc = ",,,,,,,,,,,,,,,paul,2000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
var arr = abc.replace(/^\,+/, '').split(",");
var results = [];
for (var i = 0; i < arr.length; i = i + 3) {
results.push({
"name": arr[i],
"value": arr[i + 1],
"acc": arr[i + 2]
});
}
Demo:
var abc = ",,,,,,,,,,,,,,,paul,2000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
var arr = abc.replace(/^\,+/, '').split(",");
var results = [];
for (var i = 0; i < arr.length; i = i + 3) {
results.push({
"name": arr[i],
"value": arr[i + 1],
"acc": arr[i + 2]
});
}
console.log(results);
You are on the right track with splitting your data on ,. You can then split the data in to chunks of 3, and from there map each chunk to a dict.
var data = ",,,,,,,,,,,,,,,paul,2000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
var split = data.split(",");
var chunked = [];
while (split.length) {
chunked.push(split.splice(0,3));
}
var res = chunked.map((i) => {
if (!i[0] || !i[1] || !i[2]) {
return null;
}
return {
name: i[0],
value: i[1],
acc: i[2]
};
}).filter((i) => i !== null);
console.log(res);
You can use:
abc.replace(/,+/g, ',').replace(/^,|,$/g, '').split(',');
The regEx replaces removes the data that you are not interested in before performing the split.
or
abc.split(',').filter(Boolean);
The filter(Boolean) will remove the items from the array that could be the equivalent of false once the array has been instantiated.
EDIT:
var abc =",,,,,,,,,,,,,,,paul,2,000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
var array = abc.replace(/,+/g, ',').replace(/^,|,$/g, '').split(/,([0-9,]+),/);
array = array.filter(Boolean).reduce(function(acc, item) {
if (item.match(/^[0-9,]+$/)) {
acc.push(item);
} else {
acc = acc.concat(item.split(','));
}
return acc;
}, []);
I've got to create var with few elements:
var arrNum = [4,7,5,3,4,5,6,7,8,10]
I need to find first number that is the same in array using for loop. So it will be "4" and "4"
I need to create var sameIndex and adjust the same number to sameIndex and print after for loop
So I did loop
for(var i = 0; i < arrNum.length; i++){
console.log("")
console.log("Loop number is " + i)
if(arrNum[i] === arrNum[i]{
break
sameIndex = going[i]
}
}
console.log(sameIndex)
It's not working.
One way would be to use Array#indexOf. It returns the first index of the given element in the array:
var arrNum = [4, 7, 5, 3, 4, 5, 6, 7, 8, 10];
var i;
var sameIndex = -1;
for (i = 0; i < arrNum.length; i++) {
if (arrNum.indexOf(arrNum[i]) !== i) {
console.log('This is the second occurrence of', arrNum[i]);
sameIndex = arrNum.indexOf(arrNum[i]);
break;
}
}
console.log('The indices are', sameIndex, 'and', i);
If you're looking to get the first duplicate, then use :
var arrNum = [4,7,5,3,4,5,6,7,8,10];
function firstDuplicate(array){
var history = [];
for(var element of array){
if(history.indexOf(element)<0)
//not in the history yet
history.push(element);
else
return element;
}
return null; //or any distinctive value
}
var fDup = firstDuplicate(arrNum);
You could take a hash table and display the value.
var array = [4, 7, 5, 3, 4, 5, 6, 7, 8, 10],
hash = Object.create(null),
i;
for (i = 0; i < array.length; i++) {
if (hash[array[i]]) {
console.log('first dupe: ' + array[i]);
break;
}
hash[array[i]] = true;
}
A working script:
var arrNum = [4,7,5,3,4,5,6,7,8,10];
var sameIndex = -1, going = new Array();
for(var j = 0; j < arrNum.length; j++) {
for(var i = 1; i < arrNum.length; i++){
console.log("Loop number is " + j + ", " + i)
if(arrNum[i] === arrNum[j]){
sameIndex = i;
break;
}
}
if(sameIndex > 0) {
console.log(j, sameIndex)
break;
}
}
var arrNum = [4,7,5,3,4,5,6,7,8,10];
for(var i = 0; i < arrNum.length; i++){
for(var j = i+1; j < arrNum.length; j++) {
if(arrNum[i] === arrNum[j]) {
console.log('value: '+arrNum[i]+' index1: '+i+' index2: '+j);
}
}
}
Iterate over the original array and if the value is not in a comparison array - push it into a common numbers array. The first item in that common numbers array is the target.
var origArray = [4,7,5,3,4,5,6,7,8,10];
var newArray = [];
var commonNums = [];
origArray.forEach(function(item,i) {
newArray.indexOf(item) == -1
? newArray.push(item)
: commonNums.push({item: item, index:i});
})
if(commonNums.length > 0) {
var firstCommonNum = commonNums[0].item;
var firstCommonNumIndex = commonNums[0].index;
console.log("common number " + firstCommonNum);
console.log("Loop number is " + firstCommonNumIndex);
}
I want to duplicate certain arrays in arrays using JavaScript, for example:
var res = [["1,2,3",100,50],["4,5",75,10],["6",20,90]];
var res2 = [];
for(var z in res) {
var row = res[z];
var keys = row[0].split(',')
for(var y in keys) {
var key = keys[y];
res2.push([key,row[1]/keys.length,row[2]/keys.length]);
}
}
/*
[ [ '1', 33.333333333333336, 16.666666666666668 ],
[ '2', 33.333333333333336, 16.666666666666668 ],
[ '3', 33.333333333333336, 16.666666666666668 ],
[ '4', 37.5, 5 ],
[ '5', 37.5, 5 ],
[ '6', 20, 90 ] ]
*/
The arrays are really-really long, is it possible to do this in-place (res, without res2)?
You can use splice for a true in-place processing of res:
for (var i = 0; i < res.length; ) {
var row = res[i];
var keys = row[0].split(',')
res.splice(i, 1); // Remove old element
// Insert new elements at current position
for (var j in keys)
res.splice(i++, 0, [keys[j], row[1] / keys.length, row[2] / keys.length]);
}
Result:
[
["1", 33.333333333333336, 16.666666666666668],
["2", 33.333333333333336, 16.666666666666668],
["3", 33.333333333333336, 16.666666666666668],
["4", 37.5, 5],
["5", 37.5, 5],
["6", 20, 90]
]
EDIT:
Another trick to avoid splices is to extend the size of res and start filling it from the end to the beginning:
var n = res.length;
// Precalculate new length
var length = 0;
for (var i = 0; i < res.length; i++)
length += res[i][0].split(',').length;
// Change length of array
res.length = length;
// Start filling from end to start
for (var i = n - 1, k = length - 1; i >= 0; i--) {
var row = res[i];
var keys = row[0].split(',');
for (var j = keys.length - 1; j >= 0; j--)
res[k--] = [keys[j], row[1] / keys.length, row[2] / keys.length];
}
You can use reduce function something like this
var res = [["1,2,3",100,50],["4,5",75,10],["6",20,90]];
var r = res.reduce(function(acc,curr){
return acc.concat(curr[0].split(',').map(function(el,_,cursplit){
return [el, curr[1]/cursplit.length,curr[2]/cursplit.length];
}));
},[]);
but i'm not sure that it more readable
var res = [["1,2,3",100,50],["4,5",75,10],["6",20,90]];
var r = res.reduce(function(acc,curr){
return acc.concat(curr[0].split(',').map(function(el,_,cursplit){
return [el, curr[1]/cursplit.length,curr[2]/cursplit.length];
}));
},[]);
document.getElementById('d').innerHTML = r.join('<br>');
<div id="d"></div>
UPDATE
another variant with mutate res
for(var i=0,len=res.length;i<len;i++){
var cur = res.shift();
cur[0].split(',').forEach(function(el,_,cursplit){
res.push([el, cur[1]/cursplit.length,cur[2]/cursplit.length]);
});
}
var res = [["1,2,3",100,50],["4,5",75,10],["6",20,90]];
for(var i=0,len=res.length;i<len;i++){
var cur = res.shift();
cur[0].split(',').forEach(function(el,_,cursplit){
res.push([el, cur[1]/cursplit.length,cur[2]/cursplit.length]);
});
}
document.getElementById('d').innerHTML = res.join('<br>');
<div id="d"></div>
You can get it with Array.reduce
var res = [["1,2,3",100,50],["4,5",75,10],["6",20,90]];
res = res.reduce(function(a, b){
var parts = b[0].split(',');
parts.forEach(function(i){
a.push([i, b[1]/parts.length, b[2]/parts.length]);
});
return a;
}, []);
But, I believe this still eat double memory :)
Another solution (with res2, but more economy):
It get source entities one-by-one, process it and gc allowed to clean memory...
var res2 = [];
while (res.length) {
var cur = res.shift();
var ids = cur[0].split(',')
ids.forEach(function(i){
res2.push(i, cur[1]/ids.length, cur[2]/ids.length);
});
}
so what i have is three (or more) array with different sizes something like the following :
a ['x1' , 'x2', 'x3'];
b ['y1','y2']
c ['z1']
i want to create a string like the following :
x1,y1,z1 - x2,y2 - x3
any idea how the logic behind doing something like this?
Just because I like to be contrary (who'd a thunk it!), here's an alternative using plain for loops:
var data = [
['q1','q2'],
['x1', 'x2', 'x3'],
['y1', 'y2', 'y3', 'y4', 'y5'],
['z1']
];
var result = [];
for (var i=0, iLen=data.length; i<iLen; i++) {
temp = data[i];
for (var j=0, jLen=temp.length; j<jLen; j++) {
if (!result[j]) {
result.push([temp[j]]);
} else {
result[j].push(temp[j]);
}
}
}
alert(result.join(' - ')); // q1,x1,y1,z1 - q2,x2,y2 - x3,y3 - y4 - y5
If support for sparse arrays is required (i.e. with missing members or values of undefined or null or whatever), that's not too hard to implement.
var arrays = [a, b, c];
var groupings = [];
var idx = 0;
while (true) {
var items = [];
for (var i = 0; i < arrays.length; i++) {
if (arrays[i].length > idx) {
items.push(arrays[i][idx]);
}
}
if (!items.length)
break;
else
groupings.push(items.join(','));
idx++;
}
console.log(groupings.join(' - '));
Let's say you have an array of arrays of arbitrary number, then you can use code like this to iterate through them:
var data = [
['q1', 'q2'],
['x1', 'x2', 'x3'],
['y1', 'y2', 'y3', 'y4', 'y5'],
['z1']
];
var output = "";
var term, j = 0;
while (true) {
term = [];
for (var i = 0; i < data.length; i++) {
if (data[i].length > j) {
term.push(data[i][j]);
}
}
// if no more terms, then all arrays are exhausted
// so it's time to break out of the loop
if (term.length == 0) {
break;
}
if (output) {
output += " - ";
}
output += term.join(",");
j++;
}
alert(output);
And, a working example here: http://jsfiddle.net/jfriend00/fZKbp/