Check whether array has all identical elements using Arrays.asList() and HashSet in Java Last Updated : 18 Jun, 2019 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of N elements, the task is to check whether the array have all same (identical) elements or not without using the loop. If all elements are same then print Yes otherwise print No. Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: Yes The given array has all identical elements i.e. 2. Input: arr[] = {2, 3, 3, 3, 3, 2, 2} Output: No Approach: First, we check the size of array if the size of an array is 0 or 1 then the array is identical. If it's size is > 1 then we take a set and copy all elements of an array in a set using Arrays.asList(). Then we calculate the size of the set, if the size of the set is 1, then the array has all identical elements otherwise not. Below is the implementation of the above approach: Java // Java implementation of the approach import java.util.*; class GFG { // Generic function to check whether the given array // has all identical element or not public static <T> void checkIdentical(T array[]) { // Create the Set by passing the Array // as parameter in the constructor Set<T> set = new HashSet<>(Arrays.asList(array)); // Check the size of set, f size is 0 or 1 then // array has only identical elements if (set.size() == 1 || set.isEmpty()) { System.out.print("Yes"); } else { System.out.print("No"); } } // Driver code public static void main(String args[]) { Integer arr[] = { 2, 2, 2, 2, 2, 2 }; checkIdentical(arr); } } Output: Yes Comment More infoAdvertise with us Next Article Check whether array has all identical elements using Arrays.asList() and HashSet in Java R Rajput-Ji Follow Improve Article Tags : Java DSA Arrays Java-Arrays java-hashset +1 More Practice Tags : ArraysJava Similar Reads How to Check whether Element Exists in Java ArrayList? Java ArrayList is a resizable array, which can be found in java.util package. We can add or delete elements from an ArrayList whenever we want, unlike a built-in array. We can check whether an element exists in ArrayList in java in two ways: Using contains() methodUsing indexOf() method Method 1: Us 2 min read All elements in an array are Same or not? Given an array, check whether all elements in an array are the same or not. Examples: Input : "Geeks", "for", "Geeks" Output : Not all Elements are Same Input : 1, 1, 1, 1, 1 Output : All Elements are Same Method 1 (Hashing): We create an empty HashSet, insert all elements into it, then we finally s 5 min read Difference between ArrayList and HashSet in Java Here are couple of differences between ArrayList and HashSet. Inheritance: Implementation: Implementation : ArrayList implements List interface while HashSet implements Set interface in Java.Internal implementation: ArrayList is backed by an Array while HashSet is backed by an HashMap.Duplicates : A 3 min read Check If a Value is Present in an Array in Java Given an array of Integers and a key element. Our task is to check whether the key element is present in the array. If the element (key) is present in the array, return true; otherwise, return false.Example: Input: arr[] = [3, 5, 7, 2, 6, 10], key = 7Output: Is 7 present in the array: trueInput: arr 8 min read How to check if a key exists in a HashMap in Java Given a HashMap and a key in Java, the task is to check if this key exists in the HashMap or not. Examples: Input: HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}, key = 2 Output: true Input: HashMap: {1=G, 2=e, 3=e, 4=k, 5=s}, key = 10 Output: false Using Iterator (Not Efficient): Get the HashMap a 4 min read Remove repeated elements from ArrayList in Java Prerequisite: ArrayList in Java Given an ArrayList, the task is to remove repeated elements of the ArrayList in Java. Examples: Input: ArrayList = [1, 2, 2, 3, 4, 4, 4] Output: [1, 2, 3, 4] Input: ArrayList = [12, 23, 23, 34, 45, 45, 45, 45, 57, 67, 89] Output: [12, 23, 34, 45, 57, 67, 89] Below are 3 min read CopyOnWriteArrayList hashCode() method in Java The hashCode() method of CopyOnWriteArrayList returns the hashCode value of the list. Syntax: public int hashCode() Parameters: The function does not accepts any parameter. Return Value: The function returns the hashCode value of the list. Below programs illustrate the above function: Program 1: Jav 1 min read HashSet containsAll() method in Java with Example The containsAll() method of Java HashSet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set. Syntax: public boolean containsAll(Collection C) Parameters: The parameter C i 2 min read CopyOnWriteArrayList equals() method in Java with Examples CopyOnWriteArrayList equals() method is used to compare two lists. It compares the lists as, both lists should have the same size, and all corresponding pairs of elements in the two lists are equal. Syntax: boolean equals(Object o) Parameters: This function has a single parameter which is object to 2 min read Arrays.equals() in Java with Examples The Arrays.equals() method comes under the Arrays class in Java. It is used to check two arrays, whether single-dimensional or multi-dimensional array are equal or not. Example:Below is a simple example that uses Arrays.equals() method to check if two arrays of integers are equal or not.Java// Java 3 min read Like