Home > Net >  I have a array of string have to find all the common character present from all strings
I have a array of string have to find all the common character present from all strings

Time:10-09

I have a array of string.

let arr=["robin","rohit","roy"];

Need to find all the common character present in all the strings in array.

Output Eg: r,o

I have tried to create a function for above case with multiple loops but i want to know what should be the efficient way to achive it.

CodePudding user response:

Here's a functional solution which will work with an array of any iterable value (not just strings), and uses object identity comparison for value equality:

function findCommon (iterA, iterB) {
  const common = new Set();
  const uniqueB = new Set(iterB);
  for (const value of iterA) if (uniqueB.has(value)) common.add(value);
  return common;
}

function findAllCommon (arrayOfIter) {
  if (arrayOfIter.length === 0) return [];
  let common = new Set(arrayOfIter[0]);
  for (let i = 1; i < arrayOfIter.length; i  = 1) {
    common = findCommon(common, arrayOfIter[i]);
  }
  return [...common];
}

const arr = ['robin', 'rohit', 'roy'];

const result = findAllCommon(arr);
console.log(result);

CodePudding user response:

const arr = ["roooooobin","rohit","roy"];

const commonChars = (arr) => {
  const charsCount = arr.reduce((sum, word) => {
    const wordChars = word.split('').reduce((ws, c) => {
      ws[c] = 1;
      return ws;
    }, {});
    Object.keys(wordChars).forEach((c) => {
      sum[c] = (sum[c] || 0)   1;
    });
    return sum;
  }, {});
  return Object.keys(charsCount).filter(key => charsCount[key] === arr.length);
}

console.log(commonChars(arr));

CodePudding user response:

Okay, the idea is to count the amount of times each letter occurs but only counting 1 letter per string

let arr=["robin","rohit","roy"];

function commonLetter(array){
  var count={} //object used for counting letters total
  for(let i=0;i<array.length;i  ){
    //looping through the array
    const cache={} //same letters only counted once here
    for(let j=0;j<array[i].length;j  ){
      //looping through the string
      let letter=array[i][j]
      if(cache[letter]!==true){
        //if letter not yet counted in this string
        cache[letter]=true //well now it is counted in this string
        count[letter]=(count[letter]||0) 1
        //I don't say count[letter]   because count[letter] may not be defined yet, hence (count[letter]||0)
      }
    }
  }
  return Object.keys(count)
  .filter(letter=>count[letter]===array.length)
  .join(',')
}

//usage
console.log(commonLetter(arr))

CodePudding user response:

No matter which way you choose, you will still need to count all characters, you cannot get around O(n*2) as far as I know.

arr=["robin","rohit","roy"];
    
let commonChars = sumCommonCharacters(arr);

function sumCommonCharacters(arr) {
  data = {};
  for(let i = 0; i < arr.length; i  ) {
       for(let char in arr[i]) {
         let key = arr[i][char];
         data[key] = (data[key] != null) ? data[key] 1 : 1;
       }
  }
  return data;
}
console.log(commonChars);

CodePudding user response:

You can use an object to check for the occurrences of each character.
loop on the words in the array, then loop on the chars of each word.

let arr = ["robin","rohit","roy"];

const charsMap = {};

for (let word of arr) {
  for(let char of word) {
    if(!charsMap[char]) {
      charsMap[char] = 1;
    } else {
      charsMap[char] = charsMap[char]   1;
    }

    if(charsMap[char] === arr.length) {
      console.log(char, charsMap[char]);
    } 
  }
}

  • Related