0

Trying to print out any values that are duplicates. Having an issue visualizing what I need to do. This isn't working.

This is different than any other similar question because I want all the duplicate values to be printed, not just ones that have duplicates.

Could you also give the time/space complexity of the result as well?

This should print out 2, 2, 4, 4, 7, 7

const printDuplicates = (arr) => {
  let newArray = [];

  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === arr[i] + 1) {
      newArray.push(arr[i]);
    }
  }
  return newArray
}

console.log(printDuplicates([1, 2, 2, 4, 4, 5, 6, 7, 7, 8, 9]));

And BTW I APPRECIATE ALL THE HELP

mph85
  • 1,276
  • 4
  • 19
  • 39

5 Answers5

1

Here's one way - though I must admit it isn't the most elegant but most easy to understand:

const printDuplicates = (arr) => {
  let newArray = [];
  let valToCompare;
  for (let i = 0; i < arr.length; i++) {
    valToCompare = arr[i];
    for (let j = 0; j < arr.length; j++) {
      if (i != j && arr[j] == valToCompare) {
        newArray.push(arr[j]);
      }
    }
  }
  return newArray;
}

console.log(printDuplicates([1, 2, 2, 4, 4, 5, 6, 7, 7, 8, 9]));
obscure
  • 11,916
  • 2
  • 17
  • 36
1

There are many ways to do this, here's one example using Array.filter

const printDuplicates = (arr) => {
  let newArray = [];

  for (let i = 0; i < arr.length; i++) {
    let targetValue = arr[i];
    let numFound = arr.filter(val => val === targetValue).length;
    let results = {
      targetValue,
      numFound
    }
    newArray.push( results );
    i += ( numFound - 1 );
  }
  return newArray
}

console.log(printDuplicates([1, 2, 2, 4, 4, 5, 6, 7, 7, 8, 9]));
Steven Stark
  • 1,249
  • 11
  • 19
1

If the duplicates are adjacents, and you want to display them including repeats.

const printDuplicates = (arr) => arr.filter((_,indx) => arr[indx] === arr[indx+1] || arr[indx] === arr[indx-1]);

console.log(printDuplicates([1, 2, 2, 4, 4, 5, 6, 7, 7, 8, 9]));
Zeyad Etman
  • 2,250
  • 5
  • 25
  • 42
0

Try this:

let arr = [1, 2, 2, 4, 4, 5, 6, 7, 7, 8, 9];
result = arr.filter(x => arr.filter(y => y == x).length > 1)
console.log(result);

The filter function takes a function as argument. This function takes each element of the array as argument and should return a boolean value, indicating whether the element should be included in the resulting array or not.

So we use a function that counts the occurrences of the element in the array, and returns true if it appeared more than once, or else returns false.

This occurrence counter also is written using filter function.

UPDATE:

This part, is just a function that counts the occurrences of the x element in arr:

arr.filter(y => y == x).length

So we use it in the filter function, to decide if we want to keep an element or not (if it appeared more than once then keep it). In order to keep an element, we should return true for it in filter function.

Amir
  • 1,885
  • 3
  • 19
  • 36
  • 2
    You need to add more information about the code – JEY Mar 12 '19 at 19:34
  • @JEY Thanks, checkout my edit. – Amir Mar 12 '19 at 19:40
  • so you're filtering the filtered results? – mph85 Mar 12 '19 at 20:20
  • no, the internal `filter` s just used for counting the occurrences. As in it filters the array and counts how many times the element is appeared. then the first filter use this value to decide if it wants to include the element or not – Amir Mar 12 '19 at 20:24
  • @mph85 Check my update – Amir Mar 12 '19 at 20:27
  • ah icic thank you for that, makes sense. I'm also looking for the time/space complexity as well. I kinda know how to calculate time, but not space complexity – mph85 Mar 12 '19 at 20:31
  • @mph85 well if you need a theoretical method, I don't think I have anything to offer. But if you just want a practical answer, I think you can use a very very large array as test case, and monitor the usage of RAM while running each algorithm – Amir Mar 12 '19 at 20:34
  • hm nothing crazy, just a simple explanation. Maybe something like since I'm filtering the first time, that should be O(n), and since we're running that filter again, maybe O(n2)? – mph85 Mar 12 '19 at 20:43
  • @mph85 well this is time complexity and since you said you know about that, I was talking about space complexity. But anyway, I think you're right and it is o(n2) – Amir Mar 12 '19 at 20:45
  • i could be mistaken, but i was told that maybe in this case, the time and space complexity would be the same. – mph85 Mar 12 '19 at 20:56
  • @mph85 Sorry, I don't know about that – Amir Mar 12 '19 at 20:57
  • 1
    no worries, i appreciate the help, ive accepted your answer – mph85 Mar 12 '19 at 21:02
  • quick question, what is this part `arr.filter(y => y == x).length > 1`, the `.length greater than 1` all about? – mph85 Mar 14 '19 at 03:48
  • @mph85 number of occurrences greater than 1 – Amir Mar 14 '19 at 06:53
  • @Amir so basically, this part `arr.filter(y => y === x).length > 1` is saying I want to filter all the values that are duplicate or that are equal to each other that occur more than once? and the outer filter wants to return those filtered results? – mph85 Mar 14 '19 at 06:57
-2

O(2n) time complexity (details here)

function printDuplicates(a) {
  let r={}; 
  a.map(a=>!(1-(r[a]=++r[a]|0)) );
  return a.filter(x=>r[x]);
}

console.log(printDuplicates([1, 2, 2, 4, 4, 5, 6, 7, 7, 8, 9]))
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345