Home > Mobile >  how to find duplicate strings in array (['a', 'a', 'aa', 'a'
how to find duplicate strings in array (['a', 'a', 'aa', 'a'

Time:02-23

I am learning JS, and I have homework. I am asked to transform array into new array where each item is represented by the running count of element appearances.

For example

[1, 2, 1, 1, 3]

becomes

[1, 1, 2, 3, 1]

I wrote a code which works for numbers, but fails tests with strings:

UPDATE: IT works for some numbers, for others does not :/

function duplicates(arr) {

  let i, j, newArr = [],
    count = 1;

  for (i = 0; i < arr.length; i  ) {
    for (j = 0; j < arr.length; j  ) {
      if (i == j) {
        continue
      }
      if (arr[i] === arr[j]) {
        newArr.push(count  )
        break
      }
    }
    if (j === arr.length) {
      newArr.push(1)
    }
  }
  return newArr
}

console.log(duplicates(['a', 'a', 'aa', 'a', 'aa'])) //[ 1, 2, 1, 3, 2]  <-- FAILS
console.log(duplicates([1, 2, 1, 2, 3, 1])) //[1, 2, 3, 4, 1, 5]  <-- fails
console.log(duplicates([1, 2, 1, 1, 3])) //[ 1, 1, 2, 3, 2, 1 ]  <-- MY CODE WORKS

Can you give me a hint? :/

Thank you!

CodePudding user response:

One approach is to use .map(), .slice() and .filter()

const duplicates = (nums) =>
  nums.map((value, index) => {
    const segment = nums.slice(0,index 1);
    return segment.filter(v => v === value).length;
  });

console.log(duplicates([1, 2, 1, 1, 3]));
console.log(duplicates([1, 2, 1, 2, 3, 1]));
console.log(duplicates(['a', 'a', 'aa', 'a', 'aa']));

  1. map creates a new array by iterating through nums and transforming each value via a function
  2. slice is used to create a new array based on nums. In first example, the new array is [1] in first iteration, [1,2] in second, followed by [1,2,1] and so on.
  3. filter finds the items in the array from #2 that match the current value.

CodePudding user response:

Elaborating on @CRice and @Kaiido idea, let's create an object that is creating the count of the items while you're looping through the array:

function duplicates(arr) {
  const obj = {};
  let value = 0;
  let newArr = [];
  for (i = 0; i < arr.length; i  ) {
    value = arr[i];
    if (obj[value]){
       obj[value] = obj[value]   1;
    }
    else{
       obj[value] = 1;
    }
    newArr.push(obj[value]);
  }
  return newArr
}


console.log(duplicates(['a', 'a', 'aa', 'a', 'aa'])) //[ 1, 2, 1, 3, 2]  <-- FAILS
console.log(duplicates([1, 2, 1, 2, 3, 1])) //[1, 2, 3, 4, 1, 5]  <-- fails
console.log(duplicates([1, 2, 1, 1, 3])) //[ 1, 1, 2, 3, 2, 1 ]  <-- MY CODE WORKS

JS has a nice built-in, reduce, that does so in a simpler way:

const duplicates = (arr) => {
  const obj = {}
  return arr.reduce ( (acc,cur) => {
    obj[cur] = (obj[cur])?obj[cur] 1:1
    acc.push(obj[cur])
    return acc
  }, []);

}

console.log(duplicates(['a', 'a', 'aa', 'a', 'aa'])) //[ 1, 2, 1, 3, 2]  <-- FAILS
console.log(duplicates([1, 2, 1, 2, 3, 1])) //[1, 2, 3, 4, 1, 5]  <-- fails
console.log(duplicates([1, 2, 1, 1, 3])) //[ 1, 1, 2, 3, 2, 1 ]  <-- MY CODE WORKS

  • Related