Check if two arrays contain the same elements?
Example:
Input: a1 = { 1, 3, 4 }, a2 ={ 1, 4, 3 }
Output: Both are same array
Approach: int array
Java
import java.util.Arrays;public class SameArrayCheck {public static void main(String[] args) {int[] a1 = new int[] { 1, 3, 4 };int[] a2 = new int[] { 1, 4, 3 };if (checkTwoArraySame(a1, a2)) {System.out.println("Both are same array");} else {System.out.println("Not same array");}}// check two array is same or notprivate static boolean checkTwoArraySame(int[] a1, int a2[]) {// base condition checkif (a1.length != a2.length)return false;// sort both array in asc orderArrays.sort(a1);Arrays.sort(a2);for (int i = 0; i < a1.length; i++) {if (a1[i] != a2[i])return false;}return true;}}
Approach: Object of array
Java
import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class SameArrayCheck {public static void main(String[] args) {Object[] arro1 = new Object[] { 1, 123 };Object[] arro2 = new Object[] { 1, 123 };System.out.println(Arrays.equals(arro1, arro2));}}
Approach: List of array
Java
import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class SameArrayCheck {public static void main(String[] args) {List<Integer> l1 = new ArrayList<>();l1.add(1);l1.add(123);List<Integer> l2 = new ArrayList<>();l2.add(1);l2.add(123);System.out.println(l1.equals(l2));}}
Approach: List of Person Object
Java
import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.List;public class SameArrayCheck {public static void main(String[] args) {List<Person> p1 = new ArrayList<Person>();p1.add(new Person("RAM", 24));p1.add(new Person("RAM", 22));p1.add(new Person("Shyam", 25));List<Person> p2 = new ArrayList<Person>();p2.add(new Person("Shyam", 25));p2.add(new Person("RAM", 24));p2.add(new Person("RAM", 22));Collections.sort(p1, new Comparator<Person>() {@Overridepublic int compare(Person o1, Person o2) {if (o1.getName().compareTo(o2.getName()) == 0) {if (o1.getAge() == o2.getAge())return 0;elsereturn -1;} else {return o1.getName().compareTo(o2.getName());}}});Collections.sort(p2, new Comparator<Person>() {@Overridepublic int compare(Person o1, Person o2) {if (o1.getName().compareTo(o2.getName()) == 0) {if (o1.getAge() == o2.getAge())return 0;elsereturn -1;} else {return o1.getName().compareTo(o2.getName());}}});System.out.println(p1.toString());System.out.println(p2.toString());System.out.println(p1.equals(p2));}}class Person {private String name;private int age;public Person(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + "]";}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Person other = (Person) obj;if (age != other.age)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}}
Output:
[Person [name=RAM, age=22], Person [name=RAM, age=24], Person [name=Shyam, age=25]][Person [name=RAM, age=22], Person [name=RAM, age=24], Person [name=Shyam, age=25]]true
No comments:
Post a Comment