Efficient ways to compare a variable with multiple values
In this article, we will discuss the ways to compare a variable with values.
Method 1: The idea is to compare each variable individually to all the multiple values at a time.
Program 1:
C++
#include <iostream>
using namespace std;
int main()
{
char character = 'a' ;
if (character == ( 'a' || 'e'
|| 'i' || 'o'
|| 'u' )) {
cout << "Vowel" ;
}
else {
cout << "Consonant" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void main(String[] args)
{
char character = 'a' ;
if (character == ( 'a' || 'e'
|| 'i' || 'o'
|| 'u' )) {
System.out.print( "Vowel" );
}
else {
System.out.print( "Consonant" );
}
}
}
|
C#
using System;
class GFG
{
public static void Main(String[] args)
{
char character = 'a' ;
if (character == ( 'a' || 'e'
|| 'i' || 'o'
|| 'u' )) {
Console.Write( "Vowel" );
}
else {
Console.Write( "Consonant" );
}
}
}
|
Javascript
<script>
var character = 'a' ;
if (character == ( 'a' . charCodeAt(0) ||
'e' . charCodeAt(0) ||
'i' . charCodeAt(0) ||
'o' . charCodeAt(0) ||
'u' . charCodeAt(0)))
{
document.write( "Vowel" );
}
else
{
document.write( "Consonant" );
}
</script>
|
Python3
if __name__ = = '__main__' :
character = 'a' ;
if (character = = ( 'a' or 'e' or 'i' or 'o' or 'u' )):
print ( "Vowel" );
else :
print ( "Consonant" );
|
Time Complexity: O(1) because constant operations are being performed
Auxiliary Space: O(1)
Explanation:
The above code gives the Wrong Answer or error as comparing variable in the above way is incorrect and it forced to code in the below way:
C++
#include <iostream>
using namespace std;
int main()
{
char character = 'a' ;
if (character == 'a'
|| character == 'e'
|| character == 'i'
|| character == 'o'
|| character == 'u' ) {
cout << "Vowel" ;
}
else {
cout << "Consonant" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void main(String[] args)
{
char character = 'a' ;
if (character == 'a' || character == 'e' ||
character == 'i' || character == 'o' ||
character == 'u' )
{
System.out.print( "Vowel" );
}
else
{
System.out.print( "Consonant" );
}
}
}
|
C#
using System;
class GFG{
public static void Main(String[] args)
{
char character = 'a' ;
if (character == 'a' || character == 'e' ||
character == 'i' || character == 'o' ||
character == 'u' )
{
Console.Write( "Vowel" );
}
else
{
Console.Write( "Consonant" );
}
}
}
|
Javascript
<script>
function checkCharacter(character)
{
if (character == 'a' || character == 'e' ||
character == 'i' || character == 'o' ||
character == 'u' ) {
document.write( "Vowel" );
}
else {
document.write( "Consonant" );
}
}
checkCharacter( 'a' );
</script>
|
Python3
if __name__ = = '__main__' :
character = 'a' ;
if (character = = 'a' or
character = = 'e' or
character = = 'i' or
character = = 'o' or
character = = 'u' ):
print ( "Vowel" );
else :
print ( "Consonant" );
|
Time Complexity: O(1) because constant operations are being performed
Auxiliary Space: O(1)
Method 2 – using Bitmasking: Another approach is to check among multiple groups of values and then create a bitmask of the values and then check for that bit to be set.
Program 2:
C++
#include <iostream>
using namespace std;
int main()
{
unsigned group_1 = (1 << 1) | (1 << 2) | (1 << 3);
unsigned group_2 = (1 << 4) | (1 << 5) | (1 << 6);
unsigned group_3 = (1 << 7) | (1 << 8) | (1 << 9);
int value_to_check = 9;
if ((1 << value_to_check)
& group_1) {
cout << "found a match in group 1" ;
}
if ((1 << value_to_check)
& group_2) {
cout << "found a match in group 2" ;
}
if ((1 << value_to_check)
& group_3) {
cout << "found a match in group 3" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void main(String[] args)
{
int group_1 = ( 1 << 1 ) |
( 1 << 2 ) | ( 1 << 3 );
int group_2 = ( 1 << 4 ) |
( 1 << 5 ) | ( 1 << 6 );
int group_3 = ( 1 << 7 ) |
( 1 << 8 ) | ( 1 << 9 );
int value_to_check = 9 ;
if ((( 1 << value_to_check) &
group_1) > 0 )
{
System.out.print( "found a match " +
"in group 1" );
}
if ((( 1 << value_to_check) &
group_2) > 0 )
{
System.out.print( "found a match " +
"in group 2" );
}
if ((( 1 << value_to_check) &
group_3) > 0 )
{
System.out.print( "found a match " +
"in group 3" );
}
}
}
|
C#
using System;
class GFG{
public static void Main(String[] args)
{
int group_1 = (1 << 1) |
(1 << 2) |
(1 << 3);
int group_2 = (1 << 4) |
(1 << 5) |
(1 << 6);
int group_3 = (1 << 7) |
(1 << 8) |
(1 << 9);
int value_to_check = 9;
if (((1 << value_to_check) &
group_1) > 0)
{
Console.Write( "found a match " +
"in group 1" );
}
if (((1 << value_to_check) &
group_2) > 0)
{
Console.Write( "found a match " +
"in group 2" );
}
if (((1 << value_to_check) &
group_3) > 0)
{
Console.Write( "found a match " +
"in group 3" );
}
}
}
|
Javascript
<script>
let group_1 = (1 << 1) |
(1 << 2) | (1 << 3);
let group_2 = (1 << 4) |
(1 << 5) | (1 << 6);
let group_3 = (1 << 7) |
(1 << 8) | (1 << 9);
let value_to_check = 9;
if (((1 << value_to_check) &
group_1) > 0)
{
document.write( "found a match " +
"in group 1" );
}
if (((1 << value_to_check) &
group_2) > 0)
{
document.write( "found a match " +
"in group 2" );
}
if (((1 << value_to_check) &
group_3) > 0)
{
document.write( "found a match " +
"in group 3" );
}
</script>
|
Python3
if __name__ = = '__main__' :
group_1 = ( 1 << 1 ) | ( 1 << 2 ) | ( 1 << 3 )
group_2 = ( 1 << 4 ) | ( 1 << 5 ) | ( 1 << 6 )
group_3 = ( 1 << 7 ) | ( 1 << 8 ) | ( 1 << 9 )
value_to_check = 9
if ((( 1 << value_to_check) & group_1) > 0 ):
print ( "found a match " + "in group 1" )
if ((( 1 << value_to_check) & group_2) > 0 ):
print ( "found a match " + "in group 2" )
if ((( 1 << value_to_check) & group_3) > 0 ):
print ( "found a match " + "in group 3" )
|
Output
found a match in group 3
Time Complexity: O(1) because constant operations are being performed
Auxiliary Space: O(1)
Note: This approach works best for values that don’t exceed the natural size of your CPU. It would typically be 64 in modern times. The general solution to this problem using Template from C++11. Below is the program for the same:
Program 3:
C++
#include <algorithm>
#include <initializer_list>
#include <iostream>
using namespace std;
template < typename T>
bool is_in( const T& v,
std::initializer_list<T> lst)
{
return (std::find(std::begin(lst),
std::end(lst), v)
!= std::end(lst));
}
int main()
{
int num = 10;
if (is_in(num, { 1, 2, 3 }))
cout << "Found in group" << endl;
else
cout << "Not in group" << endl;
char c = 'a' ;
if (is_in(c, { 'x' , 'a' , 'c' }))
cout << "Found in group" << endl;
else
cout << "Not in group" << endl;
return 0;
}
|
Java
import java.util.Arrays;
import java.util.List;
public class Main {
static <T> boolean is_in(T v, List<T> lst)
{
return lst.contains(v);
}
public static void main(String[] args)
{
int num = 10 ;
if (is_in(num, Arrays.asList( 1 , 2 , 3 )))
System.out.println( "Found in group" );
else
System.out.println( "Not in group" );
char c = 'a' ;
if (is_in(c, Arrays.asList( 'x' , 'a' , 'c' )))
System.out.println( "Found in group" );
else
System.out.println( "Not in group" );
}
}
|
C#
using System;
using System.Linq;
public static class Extensions
{
public static bool is_in<T>( this T[] array,
T target)
{
return array.Contains(target);
}
}
class GFG{
static public void Main ()
{
int num = 10;
int [] arr = { 1, 2, 3 };
if (arr.is_in(num))
Console.WriteLine( "Found in group" );
else
Console.WriteLine( "Not in group" );
char c = 'a' ;
char [] arr1 = { 'x' , 'a' , 'c' };
if (arr1.is_in(c))
Console.WriteLine( "Found in group" );
else
Console.WriteLine( "Not in group" );
}
}
|
Javascript
function isIn(v, arr) {
return arr.includes(v);
}
let num = 10;
if (isIn(num, [1, 2, 3])) {
console.log( "Found in group" );
} else {
console.log( "Not in group" );
}
let c = 'a' ;
if (isIn(c, [ 'x' , 'a' , 'c' ])) {
console.log( "Found in group" );
} else {
console.log( "Not in group" );
}
|
Python3
import math
def is_in(v, lst):
return v in lst
num = 10
if (is_in(num, [ 1 , 2 , 3 ] )):
print ( "Found in group" )
else :
print ( "Not in group" )
c = 'a'
if (is_in(c, [ 'x' , 'a' , 'c' ] )):
print ( "Found in group" )
else :
print ( "Not in group" )
|
Output
Not in group
Found in group
Time Complexity: O(1) because constant operations are being performed
Auxiliary Space: O(1)
Note: It is not very efficient though when not used with primitive types. For std::string it will produce an error. This task became really easy with C++17, as it comes with Fold Expression. This is generally called Folding which helps to express the same idea with less code and it works well with any data type. Here, “||” operator to reduce all the Boolean results to a single one which is only False if all the comparison results False. Below is the program for the same:
Program 4:
C++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
template < typename T>
bool IsIn(T first, vector<T> values)
{
return find(values.begin(), values.end(), first) != values.end();
}
int main()
{
int num = 10;
if (IsIn(num, { 1, 2, 3 }))
cout << "Found in group" << endl;
else
cout << "Not in group" << endl;
string c = "abc" ;
if (IsIn(c, { "xyz" , "bhy" , "abc" }))
cout << "Found in group" << endl;
else
cout << "Not in group" << endl;
return 0;
}
|
C#
using System;
using System.Linq;
class Program
{
static bool IsIn<T>(T first, params T[] values)
{
return values.Contains(first);
}
static void Main()
{
int num = 10;
if (IsIn(num, 1, 2, 3))
Console.WriteLine( "Found in group" );
else
Console.WriteLine( "Not in group" );
string c = "abc" ;
if (IsIn(c, "xyz" , "bhy" , "abc" ))
Console.WriteLine( "Found in group" );
else
Console.WriteLine( "Not in group" );
}
}
|
Javascript
function is_in(first, ...t) {
return t.includes(first);
}
let num = 10;
if (is_in(num, 1, 2, 3))
console.log( "Found in group" );
else
console.log( "Not in group" );
let c = "abc" ;
if (is_in(c, "xyz" , "bhy" , "abc" ))
console.log( "Found in group" );
else
console.log( "Not in group" );
|
Python3
def is_in(first, * t):
return first in t
num = 10
if is_in(num, 1 , 2 , 3 ):
print ( "Found in group" )
else :
print ( "Not in group" )
c = "abc"
if is_in(c, "xyz" , "bhy" , "abc" ):
print ( "Found in group" )
else :
print ( "Not in group" )
|
Output:Â
Â
Time Complexity: O(1) because constant operations are being performed
Auxiliary Space: O(1)Â
Last Updated :
09 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...