Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBrian Hernandez
20,285 PointsTrying to create an array of duplicate values from another array, why doesn't my code work?
I am working on this piece of code here where the goal is to create an array of all the values that are duplicated from another array. The resulting array I'd like to have should only enter the duplicated values from the first array once. The catch is I can't use any array functions or methods. This is what I have so far:
var numbers = [8,24,20,5,13,3,1,12,11,24,8,24,20,4,5,23,24,23,21,2,19,3,21,2,14,17,21,5,7,10,20,11,0,5,18,2,13,11,14,3,20,1,23,6,21,10,14,0,15,20];
var results = [];
var tempArr = [];
for (var i = 0; i <= numbers.length; i++) {
if (tempArr[numbers[i]] === undefined) {
tempArr[numbers[i]] = 1;
} else if (results[numbers[i]] === undefined) {
results.push(numbers[i]);
}
}
console.log(results);
I am getting closer to me desired output... but for some reason the results
array continues to contain multiple entries of the values that are duplicated in the numbers
array. Where am I going wrong here?
4 Answers
Brian Hernandez
20,285 PointsAfter doing some searching around, I found this cool solution to my problem I posted earlier. It uses an object to test weather a number is duplicated in any position in the array:
var numbers = [8,24,20,5,13,3,1,12,11,24,8,24,20,4,5,23,24,23,21,2,19,3,21,2,14,17,21,5,7,10,20,11,0,5,18,2,13,11,14,3,20,1,23,6,21,10,14,0,15,20];
var results = [];
var seenIt = {};
for (var i = 0; i < numbers.length; i++) {
var number = numbers[i];
seenIt[number] = 1 + (seenIt[number] || 0);
if (seenIt[number] === 2) {
results.push(number);
}
}
console.log(results);
So, as the loop progresses through the array, the value at the current index position of the numbers
array is used to create a key for the seenIt
object. That key is given the sum value of 1 plus either what the current value of the seenIt[number]
key is, if it exists, or 0. So, if the value of the current numbers
array index has not been seen before, its corresponding seenIt[number]
key is set to the value of 1. And if it was seen before, its prior value would have been set to 1 so the current value then gets evaluated to 2 (1 + 1 = 2). And if it is 2, then that index value gets pushed into the results
array. And when we print the results
array, we will see all of the values, given only once, that have at least one or more duplicates in the numbers
array.
Seth Kroger
56,412 Pointsresults[numbers[i]]
doesn't correlate to where the number is in the results
because the duplicate number is always pushed to the end of the array, not put into the index equal to the number. I think you could count the times you encounter a number by incrementing the spot in tempArr
and only pushing the number to results
on the 2nd encounter.
Antonio De Rose
20,884 PointsVery good question :)
var array = [24,20,5,13,3,1,12,11,24,8,24,20,4,5,23,24,23,21,2,19,3,21,2,14,17,21,5,7,10,20,11,0,5,18,2,13,11,14,3,20,1,23,6,21,10,14,0,15,20];
var a = [], prev;
array.sort();
for ( var i = 0; i < array.length; i++ ) {
if ( array[i] !== prev ) {
a.push(array[i]);
}
prev = array[i];
}
console.log(a)
Brian Hernandez
20,285 PointsThank you but I can't use a method like sort()
for this solution.
Antonio De Rose
20,884 Pointswiil this help you
var array = [24,20,5,13,3,1,12,11,24,8,24,20,4,5,23,24,23,21,2,19,3,21,2,14,17,21,5,7,10,20,11,0,5,18,2,13,11,14,3,20,1,23,6,21,10,14,0,15,20];
var a = [];var b=[];
for ( var i = 0; i < array.length; i++ ) {
if ( b.indexOf(array[i]) > -1 && a.indexOf(array[i]) < 0) {
a.push(array[i]);
}
b.push(array[i])
}
console.log(a)