WAP to accept n numbers in an array and display all the unique
numbers present in it using the following method.
boolean checkUnique(int x) To check if x is a unique number or not
A number is a unique number that starts and ends with the same digit
pls write this program in java(bluej)
Answers
Answer:
search
Sign In
Home
Courses
Algorithmskeyboard_arrow_down
Data Structureskeyboard_arrow_down
Languageskeyboard_arrow_down
Interview Cornerkeyboard_arrow_down
GATEkeyboard_arrow_down
CS Subjectskeyboard_arrow_down
Studentkeyboard_arrow_down
Jobskeyboard_arrow_down
GBlog
Puzzles
What's New ?
▲
Print All Distinct Elements of a given integer array
Last Updated: 30-10-2020
Approach:
1. Put all input integers to hashmap’s key
2. Print keySet outside the loop
Java
import java.util.HashMap;
public class UniqueInArray2 {
public static void main(String args[])
{
int ar[] = { 10, 5, 3, 4, 3, 5, 6 };
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
for (int i = 0; i < ar.length; i++) {
hm.put(ar[i], i);
}
// Using hm.keySet() to print output reduces time complexity. - Lokesh
System.out.println(hm.keySet());
}
}
C#
Given an integer array, print all distinct elements in array. The given array may contain duplicates and the output should print every element only once. The given array is not sorted.
Examples:
Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}
Output: 12, 10, 9, 45, 2
Input: arr[] = {1, 2, 3, 4, 5}
Output: 1, 2, 3, 4, 5
Input: arr[] = {1, 1, 1, 1, 1}
Output: 1