-1

Question Is is possible, to match a word that contains the same character in different positions of string array?

for example:- suppose i have the following array

arr1=["apply","application","apple","alphabet","killo","pipe"]

Now I want the following output of the element that have double 'p' (may be together or different position)

Desirable output:

arr1=["apply","application","apple","pipe"]

if i put 'l' then I should get output arr1=[killo] output

arr1=["apply","application","apple","alphabet","killo","pipe"]    
ans= arr1.filter(x => x.includes('p'))  
console.log(ans)

but i am getting following output

arr1=["apply","application","apple","alphabet","pipe"] 
  • the question you have mentioned is filtering the duplicate value of array and my question is totally different from that question. I want to filter the array that have repeating letter in its word. I need repeating letter words in the array only! – Arun sharma Mar 29 '22 at 10:50
  • I'm sure there will be another duplicate. Have you tried google? – Liam Mar 29 '22 at 10:51
  • I am doing google from last 5 hour but did not find the solution please help me sir. Thanks in advance – Arun sharma Mar 29 '22 at 10:53
  • You need to work on your google skills if you spent 5 hours looking but didn't find that. Literally search for `list get duplicates javascript`, top result... – Liam Mar 29 '22 at 10:55

1 Answers1

1

You can use the following regex format:

var arr1 = ["apply", "application", "apple", "alphabet", "killo", "pipe"];

function filter_dup(arr, c) {
  return arr.filter(x => x.match(new RegExp(`.*${c}.*${c}.*`)));
}

console.log(filter_dup(arr1, 'p'));
console.log(filter_dup(arr1, 'l'));

You can also use String.prototype.indexOf and String.prototype.lastIndexOf (if the first and last indices of a character are different than there are at least two occurrences of that character):

var arr1 = ["apply", "application", "apple", "alphabet", "killo", "pipe"];

function filter_dup(arr, c) {
  return arr.filter(x => x.indexOf(c) != x.lastIndexOf(c));
}

console.log(filter_dup(arr1, 'p'));
console.log(filter_dup(arr1, 'l'));
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55