Determine if two integers are equal without using comparison and arithmetic operators
This post will discuss how to determine whether two integers are equal without using comparison operators (==, !=, <, >, <=, >=) and arithmetic operators (+, -, *, /, %).
1. Using Bitwise XOR Operator
The simplest solution is to use the bitwise XOR operator. We know that for equal numbers, the XOR operator returns 0. We can make use of this fact, demonstrated below in C, Java, and Python:
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> // Determine if two integers are equal without using comparison // and arithmetic operators int checkForEquality(int x, int y) { return !(x ^ y); } int main(void) { int x = 10, y = 10; if (checkForEquality(x, y)) { printf ("x=%d is equal to y=%d\n", x, y); } else { printf ("x=%d is not equal to y=%d\n", x, y); } return 0; } |
Output:
x=10 is equal to y=10
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Main { // Determine if two integers are equal without using comparison operators // and arithmetic operators public static boolean checkForEquality(int x, int y) { return (x ^ y) == 0; } public static void main(String[] args) { int x = 10, y = 10; if (checkForEquality(x, y)) { System.out.printf("x=%d is equal to y=%d\n", x, y); } else { System.out.printf("x=%d is not equal to y=%d\n", x, y); } } } |
Output:
x=10 is equal to y=10
Python
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Determine if two integers are equal without using comparison operators # and arithmetic operators def checkForEquality(x, y): return (x ^ y) == 0 if __name__ == '__main__': x = 10 y = 10 if checkForEquality(x, y): print(f'x={x} is equal to y={y}') else: print(f'x={x} is not equal to y={y}') |
Output:
x=10 is equal to y=10
2. Using Array Index + Ternary Operator
We can also take advantage of the fact that a garbage value is assigned to a local array in C by default. The idea is to use the first number as the array index and set the value to 0. Then, check if the array is set for the second number or not.
Following is the C, Java, and Python implementation of the idea. Please note that this solution won’t work on negative numbers, consumes a lot of memory, and might access the array’s invalid indices.
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> // Determine if two integers are equal without using comparison // and arithmetic operators int checkForEquality(int x, int y) { short arr[x+1]; arr[x] = 0; return (!arr[y]) ? 1 : 0; } int main(void) { int x = 10, y = 10; if (checkForEquality(x, y)) { printf ("x=%d is equal to y=%d\n", x, y); } else { printf ("x=%d is not equal to y=%d\n", x, y); } return 0; } |
Output:
x=10 is equal to y=10
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Main { // Determine if two integers are equal without using comparison operators // and arithmetic operators public static boolean checkForEquality(int x, int y) { short[] arr = new short[x+1]; arr[x] = 0; return (arr[y] == 0); } public static void main(String[] args) { int x = 10, y = 10; if (checkForEquality(x, y)) { System.out.printf("x=%d is equal to y=%d\n", x, y); } else { System.out.printf("x=%d is not equal to y=%d\n", x, y); } } } |
Output:
x=10 is equal to y=10
3. Using Hashing
Since the previous approach consumes a lot of memory, a more space-efficient version uses a hash map. The hash-based implementation can be seen below in C++, Java, and Python:
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <unordered_map> using namespace std; // Determine if two integers are equal without using comparison // and arithmetic operators bool checkForEquality(int x, int y) { unordered_map<int, bool> map; map[x] = 1; return map[y]; } int main(void) { int x = 0, y = 2; if (checkForEquality(x, y)) { cout << "x is equal to y"; } else { cout << "x is not equal to y"; } return 0; } |
Output:
x is not equal to y
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.HashMap; import java.util.Map; class Main { // Determine if two integers are equal without using comparison operators // and arithmetic operators public static boolean checkForEquality(int x, int y) { Map<Integer, Boolean> map = new HashMap<>(); map.put(x, true); return map.containsKey(y); } public static void main(String[] args) { int x = 0, y = 2; if (checkForEquality(x, y)) { System.out.println("x is equal to y"); } else { System.out.println("x is not equal to y"); } } } |
Output:
x is not equal to y
Python
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Determine if two integers are equal without using comparison operators # and arithmetic operators def checkForEquality(x, y): dict = {} dict[x] = True return y in dict if __name__ == '__main__': x, y = 0, 2 if checkForEquality(x, y): print('x is equal to y') else: print('x is not equal to y') |
Output:
x is not equal to y
We can also use a hash set where we insert the first number into the set and then check if the second number exists in the set or not.
4. Repeated Subtraction
Here, the idea is to repeatedly subtract both numbers, say x and y, parallelly until one of the numbers becomes 0. If the numbers are equal, we should be left with x = 0 and y = 1.
Following is the C, Java, and Python program that demonstrates it. The solution uses the decrement operator (a unary operator) for repeated subtraction.
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> // Determine if two integers are equal without using comparison // and arithmetic operators int checkForEquality(int x, int y) { while (--x && --y); return !(x || y - 1); } int main(void) { int x = 5, y = 8; if (checkForEquality(x, y)) { printf ("x=%d is equal to y=%d\n", x, y); } else { printf ("x=%d is not equal to y=%d\n", x, y); } return 0; } |
Output:
x=5 is not equal to y=8
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Main { // Determine if two integers are equal without using comparison operators // and arithmetic operators public static boolean checkForEquality(int x, int y) { while (--x > 0 && --y > 0); return !(x != 0 || y - 1 != 0); } public static void main(String[] args) { int x = 5, y = 8; if (checkForEquality(x, y)) { System.out.printf("x=%d is equal to y=%d\n", x, y); } else { System.out.printf("x=%d is not equal to y=%d\n", x, y); } } } |
Output:
x=5 is not equal to y=8
5. Simple Subtraction
If we are permitted to use arithmetic operators, perform a simple subtraction to determine whether two integers are equal, as demonstrated below in C, Java, and Python:
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> // Determine if two integers are equal without using comparison operators int checkForEquality(int x, int y) { return !(x - y); } int main(void) { int x = 10, y = 10; if (checkForEquality(x, y)) { printf ("x=%d is equal to y=%d\n", x, y); } else { printf ("x=%d is not equal to y=%d\n", x, y); } return 0; } |
Output:
x=10 is equal to y=10
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Main { // Determine if two integers are equal without using comparison operators public static boolean checkForEquality(int x, int y) { return (x - y) == 0; } public static void main(String[] args) { int x = 10, y = 10; if (checkForEquality(x, y)) { System.out.printf("x=%d is equal to y=%d\n", x, y); } else { System.out.printf("x=%d is not equal to y=%d\n", x, y); } } } |
Output:
x=10 is equal to y=10
Python
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Determine if two integers are equal without using comparison operators def checkForEquality(x, y): return (x - y) == 0 if __name__ == '__main__': x = y = 10 if checkForEquality(x, y): print(f'x={x} is equal to y={y}') else: print(f'x={x} is not equal to y={y}') |
Output:
x=10 is equal to y=10
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)