Check The Equality Between Two Arrays Using Java

 package demo;


import java.util.HashMap;

import java.util.Map;


public class CheckArraysEquality {

public static void main(String[] args) {

int arr1[] = { 2, 1, 0, 7, 8, 9 };

int arr2[] = { 1, 0, 2, 9, 8, 7 };

System.out.println(isArraysEqual(arr1, arr2));

}


private static boolean isArraysEqual(int[] arr1, int[] arr2) {


Map<Integer, Integer> map = new HashMap<>();

for (int i : arr1) {

if (map.containsKey(i)) {

int count = map.get(i);

count++;

map.put(i, count);


} else {

map.put(i, 1);

}

}


for (int j : arr2) {

if (map.containsKey(j)) {

map.remove(j);

} else {

return false;

}

}

return true;

}

}


Comments

Popular posts from this blog

3 Lines of Code And You Can Download Your Favourite Youtube Video.

Leetcode Problem(Easy) Roman to Integer

Leetcode : (Find Minimum in Rotated Sorted Array)