How to Remove Duplicates From Array

In this example, we’ll see how to remove duplicates from array:

Input:
int[] arr1 = { 1, 2, 5, 7, 7, 8, 2, 14 };

Output:
[1, 2, 5, 7, 8, 14]

1. Java

public class RemoveDuplicatesFromArray {

	public static void main(String[] args) {
		int arr[] = { 5, 6, 1, 2, 2, 4, 5, 3 };
		RemoveDuplicatesFromArray obj = new RemoveDuplicatesFromArray();
		obj.removeDuplicatesUsingSet(arr);

	}
	public void removeDuplicatesUsingSet(int[] arr) {
		Set<Integer> obj = new HashSet<>();

		for (int i : arr) {
			obj.add(i);
		}		
		System.out.println(obj);
	}
}

Output:

[1, 2, 3, 4, 5, 6]

Complete Java Solutions can be found here: Github Link

The complete list can be found here: Practise Programming Questions

Leave a Comment