How to check the array elements is unique or not in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to check the array elements is unique or not in java?

10th Dec 2019, 5:17 AM
Cosmo
Cosmo - avatar
3 Answers
+ 3
// You can do it by converting the array into a set and by checking length of array and set are equal or not: import java.util.*; public class Qna { public static void main(String[ ] args) { Integer[] arr = {1,2,3,4,4}; Set<Integer> st = new HashSet<Integer>(Arrays.asList(arr)); System.out.print("Has Unique Elements? "); System.out.println(arr.length==st.size()?true:false); } }
10th Dec 2019, 7:08 AM
VEDANG
VEDANG - avatar
+ 2
loop through all of the element, then check one by one
10th Dec 2019, 5:27 AM
Taste
Taste - avatar
10th Dec 2019, 7:33 AM
Avinesh
Avinesh - avatar