gravityboy
gravityboy

Reputation: 817

Find unique value in at least two strings

if I have 10 different strings and I want to find if at least 2 that contain some value... what would the logic be on that?

all "and's" does not work and neither does all "or's"

Below won't work...

This will only work if every one has value...

if(str1 && str2 && str3 && str4 && str5 && str6 && str7 && str8 && str9 && str10) 

This will be true if there is only one value... needs to be at least 2

if(str1 || str2 || str3 || str4 || str5 || str6 || str7 || str8 || str8 || str10)

Is there any easy way to do this?

Edit: More info...

There is going to be ten long strings and I need to chunk through them and check (for instance) if any of the first characters are the same, then check if any of the second characters are the same etc. but I can add all the extra "charcode at" stuff in later. The same answer will work for checking full length strings and finding if two are the same.

Upvotes: 2

Views: 1428

Answers (5)

Norbert Hartl
Norbert Hartl

Reputation: 10841

To test the number of strings that are not empty it is easy to build an array of those strings (let's call it arrayOfStrings). The number of not empty strings you can find with

arrayOfStrings.filter(function(s) { return s }).length

I hope this is what you were looking for. To explain it: filter returns all elements of the array where the function returns true. The filter function gets the string as an argument. We just return it and while javascript is coercing the value into a boolean every empty string will evaluate to false. This leaves only the not empty ones in the result. A length of the resulting array is the number you are looking for and you compare it to your wanted minimum

Upvotes: 0

Adithya Surampudi
Adithya Surampudi

Reputation: 4454

Put all strings in an array say myArray and count strings having the value and see if count is greater than or equal to 2

var count = 0;
for (var index=0; index<myArray.length; index++) {
 if(myArray[index].indexOf(value) != -1) {count++;}
 if(count==2) break;
 }
if(count==2){alert("atleast 2 have value");}
else {alert("Less than 2 have value");}

Upvotes: 2

Cristian Sanchez
Cristian Sanchez

Reputation: 32097

It's a little unclear what you're asking. If you want to check if at least two of the strings are duplicates of each other, then the following should do:

function duplicatesExist (array) {
   var hash = {};
   for ( var i = 0 ; i < array.length ; i++ ) {
      if ( hash.hasOwnProperty(array[i]) ) return true;
      else hash[array[i]] = true;
   }
   return false;
}

duplicatesExist([str1, str2, str3, ...]);

Upvotes: 1

js1568
js1568

Reputation: 7032

Add each string to an array and then iterate the array, incrementing a counter every time a match is found:

var val = "search string here";
var arr = [str1, str2, str3, str4, str5, str6, str7, str8, str9, str10];

var matches = 0;
for (var i = 0; i < arr.length; i++) {
  if (arr[i].indexOf(val) > -1 && ++matches >= 2) break;
}

if (matches >= 2) {
  alert("validates");
} else {
  alert("failed");
}

Upvotes: 1

James
James

Reputation: 22237

Make an array containing the ten strings. Create a "nonEmptyItemCount" variable (or similar) and set it to zero. Loop through the array, testing the current item, and if it has a value increment nonEmptyItemCount. When you are done looping, see if nonEmptyItemCount >= 2

Upvotes: 0

Related Questions