# All elements in an array are Same or not?

Given an array, check whether all elements in an array are 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 (Hashing) We create an empty HashSet, insert all elements into it, then we finally see if size of the HashSet is one or not.

## C++

 `//c++ program to check if all array are same or not ` `#include ` ` `  `using` `namespace` `std; ` ` `  `bool` `areSame(``int` `a[],``int` `n) ` `{ ` `    ``unordered_map<``int``,``int``> m;``//hash map to store the frequency od every ` `                             ``//element ` `     `  `    ``for``(``int` `i=0;i

## Java

 `// Java program to check if all array elements are ` `// same or not. ` `import` `java.util.Arrays; ` `import` `java.util.HashSet; ` `import` `java.util.Set; ` ` `  `public` `class` `SameElements { ` `public` `static` `boolean` `areSame(Integer arr[]) ` `    ``{ ` `        ``// Put all array elements in a HashSet ` `        ``Set s = ``new` `HashSet<>(Arrays.asList(arr)); ` ` `  `        ``// If all elements are same, size of ` `        ``// HashSet should be 1. As HashSet contains only distinct values. ` `        ``return` `(s.size() == ``1``); ` `    ``} ` ` `  `    ``// Driver code ` `public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``Integer[] arr = { ``1``, ``2``, ``3``, ``2` `}; ` `        ``if` `(areSame(arr)) ` `            ``System.out.println(``"All Elements are Same"``); ` `        ``else` `            ``System.out.println(``"Not all Elements are Same"``); ` `    ``} ` `} `

## Python3

 `# Python 3 program to check if all  ` `# array are same or not ` `def` `areSame(a, n): ` `    ``m ``=` `{i:``0` `for` `i ``in` `range``(``len``(a))} ` `     `  `    ``# hash map to store the frequency  ` `    ``# of every element ` `     `  `    ``for` `i ``in` `range``(n): ` `        ``m[a[i]] ``+``=` `1` `     `  `    ``if``(``len``(m) ``=``=` `1``): ` `        ``return` `True` `    ``else``: ` `        ``return` `False` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``1``, ``2``, ``3``, ``2``] ` `     `  `    ``n ``=` `len``(arr) ` `     `  `    ``if``(areSame(arr, n)): ` `        ``print``(``"All Elements are Same"``) ` `    ``else``: ` `        ``print``(``"Not all Elements are Same"``) ` ` `  `# This code is contributed by ` `# Shashank_Sharma `

Output:

```Not all Elements are Same
```

Time Complexity : O(n)
Auxiliary Space : O(n)

Method 2 (Compare with first) The idea is simple, we compare every other element with first. If all match with first, we return true. Else we return false.

 `// Java program to check if all array elements are ` `// same or not. ` `import` `java.util.Arrays; ` `import` `java.util.HashSet; ` `import` `java.util.Set; ` ` `  `public` `class` `SameElements { ` `public` `static` `boolean` `areSame(Integer arr[]) ` `    ``{ ` `       ``Integer first = arr[``0``]; ` `       ``for` `(``int` `i=``1``; i

Output:

```Not all Elements are Same
```

Time Complexity : O(n)
Auxiliary Space : O(1)

My Personal Notes arrow_drop_up सर्वशक्तिशाली इकलौता

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.