2

Here is my question...

Given an array populated with numbers as a function parameter, produce a resulting array which contains any duplicates number from the array. For example, given the array [ 1, 2, 4, 4, 3, 3, 1, 5, 3 ] it should return [1, 4, 3]. For extra bonus points return a sorted array.

I am starting out with Javascript - I know the language however, using it in the correct way ( as one should ) I'm still getting to grips with.

My pseudo code for this would be to:

Create an array with the numbers above var numbers = [1, 2, 4, 4, 3, 3, 1, 5, 3];

Then create an empty array named "result" var result = [];

Create a for loop that goes through the var numbers to check for duplicates which will then populate the empty array "result" with the duplicates

for (var i = 0;i < numbers.length; i++) {
 //This is where I'm stuck...
}

I'm not sure what to do within the for loop to populate the var result and to throw in to the mix... The given array has to be a function parameter which makes sense so you can change the numbers in one place.

Any feedback on my thought process on this so far is greatly appreciated but ultimately I am wanting to learn how to achieve this.

Here is a JSFiddle of my progress so far... http://jsfiddle.net/fbauW/

Filth
  • 3,116
  • 14
  • 51
  • 79
  • what the hell...extra bonus points..downvote..google it you would get 10 different answers like that on stackoverflow... – HIRA THAKUR Jul 31 '13 at 11:28
  • Wow I really wasn't expecting a negative response like this... I more thought it would show that I am trying and I'm actively seeking help so I can learn and understand... isn't that part of being a developer? Or have you two forgotten this... – Filth Jul 31 '13 at 11:30
  • 1
    dont get offended friend,SO already have such questions answered!!!have a look...and if you wanted to learn something in real terms you would have tried something...you dint show your code..coz you dint try anything..and when you say `extra bonus points` what does that mean..how can you give extra bonus points..please explain – HIRA THAKUR Jul 31 '13 at 11:31
  • 1
    This is an assignment and you will see above I have given a pseudo code example of my thought process regarding my approach to the question. I have reached a wall - simply come to stack for help and understanding to learn... Ideally - not having to justify the question would be great - it's not like there is lack of information. You've seen something you don't like and decided to speak up about it - cool... This is my response - either help as this is what stack overflow is about or keep quiet. – Filth Jul 31 '13 at 11:41
  • I recognize that array from Rebecca Murphy's js-assessment! Even she recommends asking for help, so don't sweat it. – Jim Wharton Feb 23 '14 at 01:14
  • The `//This is where I'm stuck...` part made me laugh so loudly that I had to upvote this question. :D – Benny Code Aug 15 '16 at 11:55
  • I am coming late to the party, but looking for solutions developers suggest made me want to scream. lastIndexOf is not the way to perform search for duplicates. Measuring time for 1,000.000 items of random duplicates in an array it took over 5minutes! I have wrote a small NodeJS file with functions that search for duplicates. It searches for duplicates through 1million items in 3-4 seconds. https://github.com/mylonasg88/array_sort/blob/master/README.md – George Mylonas Oct 29 '18 at 12:19

2 Answers2

5

One way of doing this (and it's not the only way) is by checking for existing elements in the array. Take a look at JavaScript's lastIndexOf function:

http://www.w3schools.com/jsref/jsref_lastindexof_array.asp

It will return -1 if the object does not exist in your array, and if it exists, will return an index of a later position than you are in. So you can use an if statement in your loop that checks whether or not there is another index containing your number, and add it in to your results array IF AND ONLY IF the index you get back != the index you are currently on (if they equal, this means that there is only one of that element in the list).

If you need more help, comment here and I can type some code in!

Good luck!

  • Here is a JSFiddle I have created and I can't seem to figure out how to get any further on this - are you able to help? http://jsfiddle.net/fbauW/ – Filth Aug 01 '13 at 07:48
  • 2
    @Filth it seems like you haven't applied the lastIndexOf logic to your if statement yet. I modified the if statement to contain a check for: 1. Is the element alone in arr? and 2. did we already declare the element as a duplicate? If the element is not alone in arr and we didnt already add it as a duplicate, then lets add it. Here is the updated working fiddle: http://jsfiddle.net/gbzVF/1/ –  Aug 01 '13 at 11:23
  • Wow thank you very much @pixelatory - will be studying this through and through! – Filth Aug 01 '13 at 21:11
  • Thanks @Filth for posting this question again. As I had the same problem and I could not find the solution on other similar questions on stack overflow. This solution helped me as well. Also, I would like to hate those who have voted this question down. This community is more for discussion and help and not for discouragements. Also I made a small change, Also, I made a small change to it to render the result properly http://jsfiddle.net/gbzVF/10/ – user3050590 Jul 21 '15 at 11:59
1
Array.prototype.contains = function(k) {
  for ( var p in this)
    if (this[p] === k)
      return true;
  return false;
};
//this prototype function checks if an element is already in the array or not
//go through all the array and push the element to result if it is not 
//this way we can eliminate duplicates 
//result will contain the resultant array
function findDuplicates(Numbers) {
  var arrayLength = Numbers.length, i, j, result = [];
  for (i = 0; i < arrayLength; i++) {
    for (j = 0; j < arrayLength; j++) {
      if (a[i] == a[j] && i != j && !result.contains(a[i])) {
        result.push(a[i]);
      }
    }
  }
  return result;
}
Lonely
  • 613
  • 3
  • 6
  • 14
  • 4
    Code without explanation is almost always useless. – Felix Kling Jul 31 '13 at 11:27
  • 1
    how did you typed it so fast... – HIRA THAKUR Jul 31 '13 at 11:28
  • just got it as an assignment yesterday – Lonely Jul 31 '13 at 11:29
  • Thanks Lonely for this - Would you mind please explaining what's going on here so I can learn? – Filth Jul 31 '13 at 11:31
  • 2
    What Lonely is doing here is prototyping (think of it as "customizing" or "extending" a function for arrays called contains, which in itself goes through a for loop to search for the item. If it does, then it returns true, else false. –  Jul 31 '13 at 11:33
  • result.contains will search through the whole array. And the more duplicates are added to it the bigger it gets the slower is the search. For 1million items of random duplicates this will take ages searching for each item in 2 arrays. I have wrote a tested version for this. Takes miliseconds for 1million items. https://github.com/mylonasg88/array_sort/blob/master/README.md – George Mylonas Oct 29 '18 at 12:23