How to Check If Multiple Values in an Array Are Equal to One String

How to check if multiple values in an array are equal to one string?

I recommend something like this. First, we define the different winning combinations for the array. Next we go through each of the index sets (the winning combinations), getting the elements they represent as an array. Next, we check that the first item isn't null or an empty string (i.e. it's a played item), and then we check that they all match. If there's a match, it's a winning play and we return. Otherwise, it's a losing play so we try the next set. If that fails, we return null.

This will be more efficient than checking X and O individually, since we're simply looking for 3 in a row. The key part to excluding unplayed tiles is !string.IsNullOrEmpty(elements[0]).

private static string GetWinner(string[] grid)
{
var indexSets = new []
{
// Horizontal
new [] { 0, 1, 2 },
new [] { 3, 4, 5 },
new [] { 6, 7, 8 },

// Vertical
new [] { 0, 3, 6 },
new [] { 1, 4, 7 },
new [] { 2, 5, 8 },

// Diagonal
new [] { 0, 4, 8 },
new [] { 6, 4, 2 }
};

foreach (var indices in indexSets)
{
var elements = indices.Select(i => grid[i]).ToArray();
if (!string.IsNullOrEmpty(elements[0]) && elements[0] == elements[1] && elements[0] == elements[2])
{
return elements[0];
}
}

return null;
}

Usage example:

public static void Main(string[] args)
{
var testSet = new string[]
{
"X", "O", "O",
"O", "X", "O",
"O", "X", "X"
};
var winner = GetWinner(testSet);
if (winner != null)
{
Console.WriteLine($"{winner} wins!");
}
}

Try it online

Check if multiple array values are equal

Try this:

function checkArray(array){
var firstElement = array[0];
if(!firstElement) return false;

var result = true;

array.forEach(function(elem){
if(elem != firstElement) result = false;
});

return result;
}

Check if all values of array are equal

const allEqual = arr => arr.every( v => v === arr[0] )
allEqual( [1,1,1,1] ) // true

Or one-liner:

[1,1,1,1].every( (val, i, arr) => val === arr[0] )   // true

Array.prototype.every (from MDN) :
The every() method tests whether all elements in the array pass the test implemented by the provided function.

how to check if multiple array elements match given values from same method, in java

You could use the toString() method of Person to check equality in following way:

public boolean equals (Object object)
{
if (!(object instanceof Person || object == null)){
return false
}
Person per = (Person)object;
if ((this.toString()).equals(per.toString()))
{
return true;
}
return false;
}

JavaScript - include() - A check to see if multiple elements are in an array

The issue is in your if statement because includes() returns a boolean based on the string parameter. A better way of doing this would be to use something like:

if(arr.includes("TL") && arr.includes("TM") && arr.includes("TR")) {
console.log("yes");
}

If you have lots of elements in your array I would suggest something more along the lines of:

var flag = true;
for(i=0; i<arr.length; i++) {
if(!arr.includes(arr[i])) {
flag = false;
}
}
if(flag) {
console.log("yes");
}

C# How to compare multiple array values (strings) in if-statement?

In c#, you can't use

if (a == b == c)

Instead you have to be explicit:

if (a == b && b == c)

So instead of

board[0].Equals(board[1]).Equals(board[2]).Equals("X")

You have to write

board[0] == "X" && board[1] == "X" && board[2] == "X"

PHP Checking multiple values in an array - the right way?

You can do it something like this.

Solution 1:
Here we are using array_intersect for checking common values between two arrays.

<?php

ini_set('display_errors', 1);
$valuecheck = false;
$daafids=array(96,97,99,122,123,124,125,126,127,128,129,130,131,132,133);
if(count(array_intersect($prodcat, $daafids))>0)
{
$valuecheck=true;
}

Here we are using in_array to search that particular element in the array.

Solution 2:

<?php

ini_set('display_errors', 1);
$valuecheck = false;
$daafids=array(96,97,99,122,123,124,125,126,127,128,129,130,131,132,133);
foreach ($prodcat as $daafid) {
if(in_array($daafid,$daafids)){
$valuecheck=true;
break;
}
}
if ($valuecheck == true) {
echo $text_true;
}

in_array multiple values

Intersect the targets with the haystack and make sure the intersection count is equal to the target's count:

$haystack = array(...);

$target = array('foo', 'bar');

if(count(array_intersect($haystack, $target)) == count($target)){
// all of $target is in $haystack
}

Note that you only need to verify the size of the resulting intersection is the same size as the array of target values to say that $haystack is a superset of $target.

To verify that at least one value in $target is also in $haystack, you can do this check:

 if(count(array_intersect($haystack, $target)) > 0){
// at least one of $target is in $haystack
}

multiple conditions for JavaScript .includes() method

That should work even if one, and only one of the conditions is true :

var str = "bonjour le monde vive le javascript";
var arr = ['bonjour','europe', 'c++'];

function contains(target, pattern){
var value = 0;
pattern.forEach(function(word){
value = value + target.includes(word);
});
return (value === 1)
}

console.log(contains(str, arr));

Compare one String with multiple values in one expression

I found the better solution. This can be achieved through RegEx:

if (str.matches("val1|val2|val3")) {
// remaining code
}

For case insensitive matching:

if (str.matches("(?i)val1|val2|val3")) {
// remaining code
}


Related Topics



Leave a reply



Submit