Iqbal
Iqbal

Reputation: 219

Find intersection among a single Array's elements

I have an array like

[".a",".acc",".as",".b",".bad",".ca",".cat"]

These array elements are supposed to be searched in a long string.

fire consumed his plane. AAC was required to have two persons. A third employee who was present at the airport. As a result, Richard was unable to escape

So when it searches for ".acc" it finds only one result. But when it searches for ".a" it finds three results. Now the results are repeating, which is making trouble.

I want some way to remove elements ".acc" and ".as" from this array and keep "a" in the array, so that I can search only for ".a"

It is some sort of intersection among the elements of an array. but I can't figure out a straight forward solution to this issue.

Summery: I have got three items in an array with same starting characters(.a) . I want to keep the item with lowest length and remove the other two.

Upvotes: 2

Views: 110

Answers (3)

Ori Drori
Ori Drori

Reputation: 192132

USe Array.filter() to iterate the array. Use Array.every() to check that the element doesn't startsWith all other element (except itself).

const arr = [".a",".acc",".as",".b",".bad",".ca",".cat"];

const result = arr.filter(s1 => 
  arr.every(s2 => 
    s1 === s2 || !s1.startsWith(s2)
  )
);

console.log(result);

ES5 version:

var arr = [".a",".acc",".as",".b",".bad",".ca",".cat"];

var result = arr.filter(function(s1) {
  return arr.every(function(s2) {
    return s1 === s2 || !s1.startsWith(s2);
  });
});

console.log(result);

Upvotes: 2

Mohammed Ashfaq
Mohammed Ashfaq

Reputation: 3426

let inputString = ".a .as .as .acc .acc .acc REST OF STRING...";

let keywords = [".a", ".acc", ".as", ".b", ".bad", ".ca", ".cat"];

function getOccurrenceOfKeyword(searchString, keyword) {
  return searchString.split(keyword).length - 1;
}

function matchingKeywordsOccurrence(searchString, searchKeyword) {
  let matchingKeywordsOccurrence = 0;
  keywords.forEach(keyword => {
    if (keyword.startsWith(searchKeyword) && keyword !== searchKeyword) {
      matchingKeywordsOccurrence += getOccurrenceOfKeyword(
        searchString,
        keyword
      );
    }
  });
  return matchingKeywordsOccurrence;
}

function findExclusiveOccurrenceOfKeyword(searchString, searchKeyword) {
  let keywordOccurrence = getOccurrenceOfKeyword(searchString, searchKeyword);
  return (
    keywordOccurrence - matchingKeywordsOccurrence(searchString, searchKeyword)
  );
}

console.log(findExclusiveOccurrenceOfKeyword(inputString, ".a"));
console.log(findExclusiveOccurrenceOfKeyword(inputString, ".as"));
console.log(findExclusiveOccurrenceOfKeyword(inputString, ".acc"));

Upvotes: 0

cybersam
cybersam

Reputation: 67019

This will fill z with the non-overlapping strings from the original array:

let z = [];
let cur;
['.a', '.acc', '.as', '.b', '.bad', '.ca', '.cat'].sort().forEach((x) => {
  if (!x.startsWith(cur)) {
      z.push(x);
      cur = x
  }
})

(The sort() call is only needed if the original array may not always be already sorted.)

Upvotes: 2

Related Questions