WAP to Print Unique Digits Of an Integer Array

Print Unique Digits Of an Array: This Java program takes an input array of integers, removes any repeated numbers, and creates a new list containing only the unique numbers. It achieves this using a HashMap to track the occurrence of each number.

package com.softwaretestingo.interviewprograms;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class InterviewPrograms34
{
	/*
	 * List a[ ] = [2, 1,1,4,5,5,6,7] 
	 * Expected output : [2,4, 6,7] 
	 * Numbers which gets repeated we should remove and make output as above I know using HashSet
	 * we get as [2, 1,4,5 , 6,7] But interviewee asked about deleting repeated ones
	 */
	public static void main(String[] args) 
	{
		Integer arr[]= {2,1,1,4,5,5,6,7};
		List <Integer> ar = Arrays.asList(arr);
		HashMap<Integer,Integer> hm = new HashMap<>();
		ar.forEach(e ->{if(hm.containsKey(e))
		{
			hm.remove(e);
		}
		else
		{
			hm.put(e,1);
		}});
		ar = new ArrayList<>(hm.keySet());
		System.out.println(ar);
	}
}

Output

[2, 4, 6, 7]

Here’s a breakdown of the program:

  1. Define the main method:
    • The entry point of the program is the main method.
  2. Initialize the input array:
    • The input array arr is initialized with the values {2, 1, 1, 4, 5, 5, 6, 7}.
  3. Convert array to a list:
    • The program uses Arrays.asList() to convert the input array arr into a list ar.
  4. Remove repeated numbers using a HashMap:
    • The program creates a HashMap called hm to track the occurrence of each number.
    • It uses the forEach method to iterate through each element e of the list.
    • If hm already contains the element e as a key, it means e is a repeated number. In this case, the program removes the key-value pair from the hm.
    • If e is not already present in hm, it means it is a unique number, and the program puts e as the key in hm with a value of 1.
  5. Create a new list with unique numbers:
    • After processing all elements, the program creates a new ArrayList ar by extracting the keys (unique numbers) from the hm using hm.keySet().
  6. Print the modified list:
    • The program prints the modified list ar, which now contains only the unique numbers.

Overall, this program removes any repeated numbers from the input array and creates a new list with only the unique numbers. For the given input array {2, 1, 1, 4, 5, 5, 6, 7}, the output will be [2, 4, 6, 7], as all repeated numbers have been removed. The program demonstrates the use of a HashMap to efficiently handle duplicates and create a list of unique elements.

Alternative 1:

This Java program takes an input array of integers, removes any repeated numbers, and creates a new set containing only the unique numbers. It achieves this using a HashSet to efficiently track and eliminate duplicates.

package com.softwaretestingo.interviewprograms;
import java.util.HashSet;
public class InterviewPrograms34_1
{
	/*
	 * List a[ ] = [2, 1,1,4,5,5,6,7] 
	 * Expected output : [2,4, 6,7] 
	 * Numbers which gets repeated we should remove and make output as above I know using HashSet
	 * we get as [2, 1,4,5 , 6,7] But interviewee asked about deleting repeated ones
	 */
	public static void main(String[] args) 
	{
		Integer ar[]= {2,1,1,4,5,5,6,7};
		HashSet<Integer> hs= new HashSet<Integer>();
		
		for(int s1:ar)
		{
			if(hs.add(s1)==false)
			{
				hs.remove(s1);
			}
		}
		System.out.println(hs);
	}
}

Output

[2, 4, 6, 7]

Here’s a breakdown of the program:

  1. Define the main method:
    • The entry point of the program is the main method.
  2. Initialize the input array:
    • The input array ar is initialized with the values {2, 1, 1, 4, 5, 5, 6, 7}.
  3. Create a HashSet to store unique numbers:
    • The program creates an empty HashSet<Integer> called hs to store unique numbers.
  4. Remove repeated numbers using HashSet:
    • The program iterates through each element s1 of the input array ar using a for-each loop.
    • For each element, it tries to add it to the HashSet using hs.add(s1).
    • If s1 is already present in the HashSet, the add method returns false, indicating that s1 is a repeated number. In this case, the program removes s1 from the HashSet using hs.remove(s1).
  5. Print the modified set:
    • After processing all elements, the HashSet hs contains only unique numbers, as duplicates have been eliminated.
    • The program prints the modified HashSet hs, which now contains only the unique numbers.

Overall, this program removes any repeated numbers from the input array and creates a new set with only the unique numbers. For the given input array {2, 1, 1, 4, 5, 5, 6, 7}, the output will be [2, 4, 6, 7], as all repeated numbers have been removed. The program demonstrates the use of a HashSet to efficiently handle duplicates and create a set of unique elements.

Alternative Way 2:

This Java program takes an input list of integers, removes any numbers that occur more than once, and creates a new list containing only the unique numbers. It achieves this by using a HashMap to count the occurrences of each number in the input list.

package com.softwaretestingo.interviewprograms;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class InterviewPrograms34_2
{
	/*
	 * List a[ ] = [2, 1,1,4,5,5,6,7] 
	 * Expected output : [2,4, 6,7] 
	 * Numbers which gets repeated we should remove and make output as above I know using HashSet
	 * we get as [2, 1,4,5 , 6,7] But interviewee asked about deleting repeated ones
	 */
	public static void main(String[] args) 
	{
		ArrayList<Integer> intList= new ArrayList<>(Arrays.asList(2,1,1,4,5,5,6,7));
		HashMap<Integer,Integer> getCount=new HashMap<Integer,Integer>();
		for ( int c : intList ) 
		{
			if (getCount.containsKey(c)) 
			{
				getCount.put(c, getCount.get(c)+1);
			} 
			else 
			{
				getCount.put(c,1);
			}
		}
		List<Integer> newIntList= new ArrayList<Integer>();
		for (int c:intList )
		{
			if ( getCount.get(c)<2)
			{
				newIntList.add (c);
			}
		}
		System.out.println(newIntList);
	}
}

Output

[2, 4, 6, 7]

Here’s a breakdown of the program:

  1. Define the main method:
    • The entry point of the program is the main method.
  2. Initialize the input list:
    • The input list intList is initialized with the values [2, 1, 1, 4, 5, 5, 6, 7].
  3. Count occurrences of each number using a HashMap:
    • The program creates a HashMap<Integer, Integer> called getCount to count the occurrences of each number in the intList.
    • It iterates through each element c of the intList.
    • For each element, it checks if c is already a key in getCount. If yes, it increments the corresponding count value. If not, it adds c as a new key with a count value of 1.
  4. Create a new list with unique numbers:
    • After counting the occurrences of each number, the program creates a new ArrayList<Integer> called newIntList.
    • It iterates through each element c of the intList.
    • For each element, it checks if its count in getCount is less than 2 (i.e., it occurs only once). If yes, it adds c to newIntList.
  5. Print the modified list:
    • After processing all elements, newIntList contains only unique numbers, as numbers occurring more than once have been removed.
    • The program prints the modified list newIntList, which now contains only the unique numbers.

Overall, this program removes any numbers that occur more than once from the input list and creates a new list with only the unique numbers. For the given input list [2, 1, 1, 4, 5, 5, 6, 7], the output will be [2, 4, 6, 7], as all repeated numbers have been removed. The program demonstrates the use of a HashMap to efficiently handle duplicates and create a list of unique elements.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment