I have two arrays, named arr and ar. Suppose ar has 7 element and arr has 6 elements. I want to remove an element if it is the same in both, otherwise assign it to a new variable. I have this so far:
var arr = new Array(); // Elements are 65,66,67,68,69,70
var newID = new Array();
var ar = new Array(); // 64,65,66,67,68,69,70
if (ar.length != arr.length) {
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < ar.length; j++) {
if (arr[i] == ar[j]) {
delete ar[i];
arr.splice(i, 1);
break;
}
newID = ar[i];
}
}
for (var i = 0; i < ar.length; i++) {
newID = ar[i];
}
This does not work properly as it will compare with an undefinded value. Please help me correct it.
Here is one more using reduce
var arr1 = [1, 2, 3, 4, 5];
var arr2 = [1, 3, 5, 7, 9];
var result = arr1.reduce(function (prev, value) {
var isDuplicate = false;
for (var i = 0; i < arr2.length; i++) {
if (value == arr2[i]) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
prev.push(value);
}
return prev;
}, []);
alert(JSON.stringify(result.concat(arr2)));
EDITED
var arr1 = [1, 2, 3, 4, 5];
var arr2 = [1, 3, 5, 7, 9];
arr2 = arr2.reduce(function (prev, value) {
var isDuplicate = false;
for (var i = 0; i < arr1.length; i++) {
if (value == arr1[i]) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
prev.push(value);
}
return prev;
}, []);
alert(JSON.stringify(arr2));
You can try the following:
var arr = [1, 2, 3, 4, 5, 6, 7];
var ar = [2, 4, 6, 8, 10];
var newID = [];
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < ar.length; j++){
if(arr[i] == ar[j]){
newID.push(arr[i]);
arr.splice(i, 1);
ar.splice(j, 1);
break;
}
}
}
alert(arr);
alert(ar);
alert(newID);
i just want to add a js library -- lodash
var _ = require('lodash');
var array1 = [1,2,3,4,5];
var array2 = [3,1,5];
_.difference(array1,array2)
// returns [ 2, 4 ]
I'll give you the following solutions.
var ar = [1,2,3,4,5,6,7];
var arr = [3,6,7,8,9,2];
//ES5
var mergedArray = ar.concat(arr);
var newId = [];
for(var i=0;i<mergedArray.length;i++){
var id = mergedArray[i];
if(newId.indexOf(id) !== -1) continue;
newId.push(id);
}
//or smartter
var newId = ar.concat(arr).filter(function(id, pos, self) {
return self.indexOf(id) === pos;
});
//or ES6
var mergedArray = ar.concat(arr);
var newId = [];
for(let id of mergedArray){
if(newId.indexOf(id) !== -1) continue;
newId.push(id);
}
//or smarter ES6
var newId = ar.concat(arr).filter((id, pos, self) => self.indexOf(id) === pos);
The choice is yours. :)
var arr = [1, 2, 2, 3, 4, 5, 6, 7];
var ar = [2, 4, 6, 8, 10];
var combinearray = [...arr, ...ar]
var newArr = new Set(combinearray);
console.log(...newArr)
Related
Example: [1, 4, 9, 78, 42, 4, 11, 56]
Here the duplicate value is 4 and the gap is 3.
I used the array for each array element but I want this query to be optimized.
In Javascript, the following code will do the same for you.
var temp = [1, 4, 9, 78, 42, 4, 11, 56];
var encountered = [];
//This function gets you all the indexes of `val` inside `arr` array.
function getAllIndexes(arr, val) {
var indexes = [], i;
for(i = 0; i < arr.length; i++)
if (arr[i] === val)
indexes.push(i);
return indexes;
}
for(var i=0;i<temp.length;i++) {
if(encountered[temp[i]]) continue;
else {
var indexes = getAllIndexes(temp, temp[i]);
encountered[temp[i]] = true;
if(indexes.length>1) {
var steps = indexes[1]-indexes[0]-1;
$('.container').append('Duplicate item: '+temp[i]+' steps: '+ steps+'<br/>');
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div class="container"></div>
The below solution will give you if multiple duplicates.
http://jsfiddle.net/saz1d6re - Console Output
http://jsfiddle.net/saz1d6re/2/ - HTML Output
var a = [1, 4, 9, 78, 42, 4, 11, 4, 9];
result = [];
verified = [];
for(var i =0; i<a.length; i++){
b = a[i];
temp = {};
temp.value = a[i];
temp.hasDuplicate = false;
temp.positions = [];
temp.differenceFromFirstOccurence = [];
if(verified.indexOf(b) === -1){
temp.positions = [i+1];
for(var j = 0; j <a.length; j++){
c = a[j];
if( i !== j && b === c){
temp.hasDuplicate = true;
temp.positions.push(j+1);
}
}
verified.push(b);
result.push(temp);
}
}
for(var i = 0; i < result.length; i++){
if(result[i].hasDuplicate){
firstPosition = result[i]['positions'][0];
for(var j = result[i]['positions'].length-1; j > 0; j--){
diff = result[i]['positions'][j] - firstPosition-1;
result[i].differenceFromFirstOccurence.push(diff);
}
}
result[i].differenceFromFirstOccurence.reverse();
}
console.log(result);
for(var i =0; i < result.length; i++){
if(result[i].hasDuplicate && result[i].differenceFromFirstOccurence.length){
console.log("The first occurence of "+result[i].value+" is at "+ result[i].positions[0]);
for(var j = 1; j < result[i].positions.length; j++){
console.log("The duplicate occurence of "+result[i].value+" is at "+ result[i].positions[j] +" and difference is "+ result[i].differenceFromFirstOccurence[j-1]);
}
}
}
You can try this approach. First map all elements that have duplicates, then filter out empty values.
const data = [1, 4, 9, 78, 42, 4, 11, 56];
let duplIndex;
const res = data.map((el, index) => {
duplIndex = data.indexOf(el, index+1);
if(duplIndex !== -1){
return {el:el, gap: duplIndex - (index + 1)}
}
}).filter((el) => {
return el !== undefined;
});
Dear all I'm trying to find non repeated value in an array using javascript.I have written some code but it not working properly ..can you guys tell me where is the problem.thanks.
var arr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
var n = arr.length;
var result = '';
function nonrep() {
for (var i = 0; i < n; i++) {
var j;
for (j = 0; j < n; j++)
if (i != j && arr[i] == arr[j]) {
result = arr[i];
break;
}
if (j == n)
return arr[i];
}
return result;
}
console.log(nonrep())
There is possibly a more neat approach to this solution, but this works as expected by filtering the array and compare it's current value with the array items itself (expect current item's index value).
const sampleArray = [1,2,3,7,2,1,3];
const getNonDuplicatedValues = (arr) =>
arr.filter((item,index) => {
arr.splice(index,1)
const unique = !arr.includes(item)
arr.splice(index,0,item)
return unique
})
console.log("Non duplicated values: " , ...getNonDuplicatedValues(sampleArray))
Some changes:
Move all variable declarations inside of the function.
Use a function parameter for the handed over array, keep the function pure.
Declare all needed variables at top of the function in advance.
Take an array as result array unique.
Check i and j and if equal continue the (inner) loop.
Check the value at i and j and exit the (inner) loop, because a duplicate is found.
Take the check at the end of the inner loop and check the index j with the length of the array l, and if equal push the value to unique.
Use a single return statement with unique array at the end of the outer loop.
function getUnique(array) {
var l = array.length,
i, j,
unique = [];
for (i = 0; i < l; i++) {
for (j = 0; j < l; j++) {
if (i === j) {
continue;
}
if (array[i] === array[j]) {
break;
}
}
if (j === l) {
unique.push(array[i]);
}
}
return unique;
}
console.log(getUnique([-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3]));
Another solution could be to check if indexOf and lastIndexOf returns the same value. Then you found a unique value.
var array = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3],
unique = array.filter((v, i) => array.indexOf(v) === array.lastIndexOf(v));
console.log(unique);
You could first use reduce to get one object with count for each number element and then filter on Object.keys to return array of non-repeating numbers.
var arr=[-1,2,5,6,2,9,-1,6,5,-1,3];
var obj = arr.reduce((r, e) => (r[e] = (r[e] || 0) + 1, r), {});
var uniq = Object.keys(obj).filter(e => obj[e] == 1).map(Number)
console.log(uniq)
Solution with for loop.
var arr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
var uniq = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length; j++) {
if (arr[i] == arr[j] && i != j) break;
else if (j == arr.length - 1) uniq.push(arr[i])
}
}
console.log(uniq)
Another simple approach
var arr = [1,1,2,3,3,4,4,5];
let duplicateArr = [];
var repeatorCheck = (item) => {
const currentItemCount = arr.filter(val => val=== item).length;
if(currentItemCount > 1) duplicateArr.push(item);
return currentItemCount;
}
var result = arr.filter((item,index) => {
var itemRepeaterCheck = !duplicateArr.includes(item) && repeatorCheck(item);
if(itemRepeaterCheck === 1){
return item;
}
});
console.log(result);
let arr = [1, 2, 1, 3, 3, 5];
function nonRepeatableNo(arr) {
let val = []
for (let i = 0; i < arr.length; i++) {
let count = 0;
for (let j = 0; j < arr.length; j++) {
if (arr[i] === arr[j]) {
count += 1
}
}
if (count === 1) {
val.push(arr[i])
}
}
console.log(val)
}
nonRepeatableNo(arr)
const arr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
const non_repeating = arr.filter(num => arr.indexOf(num) === arr.lastIndexOf(num))
console.log(non_repeating)
Filtering only unique elements according to OP request:
This uses for loops, as requested. It returns an array containing only elements appearing once in the original array.
var arr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
var n = arr.length;
var result = [];
function nonrep() {
for (var i = 0; i < n; i++) {
for (var j=0 ; j < n; j++)
if (i!=j && arr[i]==arr[j])
break;
if(j==n)
result.push(arr[i]);
}
return result;
}
console.log(nonrep())
var arr1 = [45, 4,16,25,45,4,16, 9,7, 16, 25];
var arr=arr1.sort();
console.log(arr);
var str=[];
arr.filter(function(value){
if( arr.indexOf(value) === arr.lastIndexOf(value))
{ str.push(value);
console.log("ntttttttttttttnnn" +str)
}// how this works ===============A
})
O/P
7,9
Please try the below code snippet.
var arr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
var uniqArr = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length; j++) {
if (arr[i] == arr[j] && i != j) break;
else if (j == arr.length - 1){
uniqArr.push(arr[i])
}
}
}
console.log(uniqArr)
this ES6 code worked for me :
a.map(c=>a.filter(b=>c==b)).filter(e=>e.length<2).reduce((total, cur)=> total.concat(cur), [])
Here is a working method with loops.
var arr = [-1,2,5,6,2,9,-1,6,5,-1,3];
var len = arr.length;
const result = arr
.filter(value=>{
var count=0;
for(var i=0;i<len;i++)
{
if(arr[i]===value)
count++;
}
return count===1;
})
console.log(result);
const sampleArr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
function getUnique(arr){
const result=[]
const obj={}
for(let i=0;i<arr.length;i++){
if(!obj[arr[i]]){
obj[arr[i]]=true
result.push(arr[i])
}else{
const index= result.indexOf(arr[i])
if(index!==-1){
result.splice(result.indexOf(arr[i]),1)
}
}
}
return result
}
const uniqueArr= getUnique(sampleArr)
console.log(uniqueArr)
Here is the solution..
var x = [1,1,2,3,2,4]
var res = []
x.map(d => {
if(res.includes(d)) {
// remove value from array
res = res.filter((a) => a!=d)
} else {
// add value to array
res.push(d)
}
})
console.log(res) // [3,4]
//without using any filter also with minimum complexity
const array = [1 , 2, 3, 4, 2, 3, 1, 6, 8,1,1 ];
const unique = new Set();
const repetedTreses = new Set();
for(let i=0; i<array.length; i++) {
if(!unique.has(array[i]) && !repetedTreses.has(array[i])){
unique.add(array[i]);
}else{
repetedTreses.add(array[i]);
unique.delete(array[i]);
}
}
let uniqueElements=[...unique];
console.log(uniqueElements);
You can use filter and indexOf for that:
console.log(
[-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3].filter((v, i, a) => a.indexOf(v, i + 1) === -1 )
);
var arr = [4, 5, 6, 3, 4, 5, 2, 5, 6, 4, 2,];
function quickSort(arra) {
if (arra.length <= 1) {
return arra;
}
else {
var len = arra.length;
var left = [];
var right = [];
var temp = arra.pop();
var newarr = [];
for (var i = 1; i < len; i++) {
if (arra[i] < temp) {
left.push(arra[i]);
}
else { right.push[i]; }
}
}
return newarr.concat(quickSort(left), temp, quickSort(right));
}
console.log(quickSort(arr))
The result is:
I wonder why this method only return me one character in the array?
pop() method removes last element from array (so length decreases by 1) and first element has index 0 so you need to replace your for loop with:
for (var i = 0; i < len-1; i++) {
Also you need to change right.push[i] as mentioned in comments.
Array length is 7
Original array
var arr = [2, 4, 6];
Needed array
arr = [null,null,2,null,4,null,6];
0 is not present in array so need to replace with null,
1 is not available replace with null and
2 is available so put 2 in new array so on..
You can use the splice() method on the array
var arr=[2,4,6];
var l = arr[arr.length-1];
for(var i=0; i<=l; i++){
if(arr[i] !== i){
arr.splice(i, 0, null);
}
}
Output : [null, null, 2, null, 4, null, 6]
This modifies the original array.
I will write a permanence case for all answers soon.
function createArrayFromArray(array, length) {
var new_array = new Array(length);
for (var i = 0; i < new_array.length; i++) {
new_array[i] = null;
}
for (var i = 0; i < array.length; i++) {
new_array[array[i]] = array[i];
}
return new_array;
}
console.log(createArrayFromArray(arr, 7)); //[null, null, 2, null, 4, null, 6]
You just need to find the max value in the array and then iterate from 0 to that max, checking each value to see if it was present in the source or not:
var arr = [2, 4, 6];
var max = Math.max.apply(Math, arr);
var result = [];
for (var i = 0; i <= max; i++) {
if (arr.indexOf(i) !== -1) {
result[i] = i;
} else {
result[i] = null;
}
}
Working demo: http://jsfiddle.net/jfriend00/c7p8mkqy/
As I asked in my comments, I'd like to know what problem you're actually trying to solve because it seems like both the original and the newly created data structures are inefficient structures that could probably use different form of data and work more efficiently. But, we can only help you make a wiser choice if you explain the actual problem, rather just your attempted solution.
Given you have the only input arr which you want to fill null inside. Try this:
var arr = [2, 4, 6];
var output = [];
while (arr.length>0){
var first = arr.splice(0,1);
while (output.length<first[0])
output.push(null);
output.push(first[0]);
}
// output should be [null,null,2,null,4,null,6];
Try:
var arr = [2, 4, 6];
var new_arr = [];
var i = 0;
while(i < 7){
var pos = arr.indexOf(i++);
new_arr.push(pos !== -1 ? arr[pos] : null)
}
document.write(JSON.stringify(new_arr, null, 4))
var arr = [2, 4, 6];
var result = new Array(7);
arr.forEach(function(a) { result[a] = a;});
Interesting quiz:
var arr = [2, 4, 6]
var n = 0
while(arr.length > n) {
if(arr[n] !== n) {
arr = arr.slice(0,n).concat(null, arr.slice(n))
}
n++
}
console.log(arr) // [null, null, 2, null, 4, null, 6]
This approach applies to array consists of random number of sorted integers.
var arr = [2, 4, 6];
var narr = (new Array(arr.sort()[arr.length-1]))
arr.map(function(v){
narr[v] = v;
});
for (var i = 0; i<narr.length; i++) narr[i]||(narr[i]=null);
console.log(narr);
Try splice():
var arr = [2, 4, 6];
var i = 0,
l = arr[arr.length - 1];
while (i < l) {
if(i !== arr[i])
arr.splice(i, 0, null);
i++;
}
console.log(arr); //[ null, null, 2, null, 4, null, 6 ]
I have an array of arrays that looks like: var data = [[2, 2,3], [3, 9], [5, 6,7,8]];
(fiddle here)
I need to be able to create a new array based on each inner array's index. So from the above output I'm looking for
1 - [2,3,5]
2 - [2,9,6]
3 - [3,7]
4 - [8]
helper average method:
Array.prototype.average = function () {
var sum = this.sum();
return sum / this.length;
};
I've got something like :
var data = [[2, 2,3], [3, 9], [5, 6,7,8]];
//Sconsole.log(data);
Array.prototype.averageAll = function () {
var avgArrays = [[]];
var self = this;
for (var i = 0; i < self.length; i++) {
avgArrays[0].push(self[i][0]);
}
return avgArrays[0].average();
};
//3.333 to the console
console.log(data.averageAll());
I've hardcoded in the season here because if I try to use avgArrays[i][i] I get an error push is not defined. For my simple example, the function calculates the average of the 0th position of each array in the array. If I have arrays of varying sizes like this, how can I make this go slickly in one fell swoop?
reduce can be handy here-
var data= [[2, 2, 3], [3, 9], [5, 6, 7, 8]];
data.Average= function(itm){
return data.Sum(itm)/(itm.length);
}
data.Sum= function(itm){
return itm.reduce(function(a, b){
return a+b
});
}
data.map(data.Average);
/* returned value: (Array)
2.3333333333333335,6,6.5
*/
A comment reminded me to add a 'shim' for IE8 and lower- the other browsers get map and reduce-
(function(){
var ap= Array.prototype; //IE8 & lower
if(!ap.map){
ap.map= function(fun, scope){
var T= this, L= T.length, A= Array(L), i= 0;
if(typeof fun== 'function'){
while(i<L){
if(i in T){
A[i]= fun.call(scope, T[i], i, T);
}
++i;
}
return A;
}
};
}
if(!ap.reduce){
ap.reduce= function(fun, temp, scope){
var T= this, i= 0, len= T.length, temp;
if(typeof fun=== 'function'){
if(temp== undefined) temp= T[i++];
while(i<len){
if(i in T) temp= fun.call(scope, temp, T[i], i, T);
i++;
}
}
return temp;
}
}
})();
Array.prototype.sum = function () {
var total = 0;
for (var i = 0; i < this.length; i++) {
total += this[i];
}
return total;
};
Array.prototype.average = function () {
var sum = this.sum();
return sum / this.length;
};
var data = [[2, 2,3], [3, 9], [5, 6,7,8]];
//Sconsole.log(data);
Array.prototype.averageAll = function () {
var avgArrays = [];
var self = this;
//in an array of arrays, val is an array
var maxLen = 0;
for(var i = 0; i < self.length; i++) {
if(self[i].length > maxLen)
{
maxLen = self[i].length;
}
}
console.log('maxlen is ' + maxLen);
for(var j = 0; j < maxLen; j++) {
avgArrays.push([]);
for(var k = 0; k < self.length; k++) {
if(self[k][j]){
avgArrays[j].push(self[k][j]);
}
}
}
console.log(avgArrays);
var result = []
for (var x = 0; x < avgArrays.length; x++) {
result.push(avgArrays[x].average());
}
return result;
};
console.log(data.averageAll());
I think the following code should construct your array for you:
var data = [[2, 2,3], [3, 9], [5, 6,7,8]];
var max = 0;
for(var i = 0; i < data.length; i++) {
max = data[i].length > max ? data[i].length : max
}
var result = [];
for(var i = 0; i < max; i++) {
result[i] = [];
for(var j = 0; j < data.length; j++) {
if(i < data[j].length) {
result[i].push(data[j][i]);
}
}
}
After that, it is trivial to calculate the average:
var averages = [];
for(var i = 0; i < result.length; i++) {
var array = result[i];
var sum = 0;
for(var j = 0; j < array.length; j++) {
sum += array[j];
}
averages.push(sum / array.length);
}
Fiddle here.
So if I understood correctly, you want a new array made up of averages of your sub arrays?
Here is a simple way to do it, by leveraging built-in array functions
var data = [[2, 2,3], [3, 9], [5, 6,7,8]];
var averageAll = function(arr) {
return arr.map(function(a) {
return a.reduce(function(b,c) { return b+c; })/a.length;
});
};
averageAll(data);
// -> [2.3333333333333335, 6, 6.5]
Also, as a rule of thumb, don't mess with the standard types' prototypes, in my experience it only leads to trouble.
var data = [
[2, 2, 3],
[3, 9],
[5, 6, 7, 8]
];
Array.prototype.averageAll = function() {
var result = [],
maxIdx = Math.max.apply(Math, this.map(function(arr) { return arr.length }));
for (var i = 0; i < maxIdx; i++) {
var sum = this.reduce(function(old, cur) {
return old + (cur[i] || 0);
}, 0);
result.push(sum / this.length);
}
return result;
};
console.log(data.averageAll());
//[3.3333333333333335, 5.666666666666667, 3.3333333333333335, 2.6666666666666665]
fiddle