Remove Duplicate Item from array using HashMap in Java

 Remove Duplicate Item from array using HashMap in Java






Code:
package tutorialJava;
import java.util.*;
public class removeDuplicate {
public static ArrayList<Integer> sol(int[] arr){
ArrayList<Integer> ans = new ArrayList<>();
HashMap<Integer,Integer> map = new HashMap<>();
for(int i=0;i<arr.length;i++) {
if(!map.containsKey(arr[i])) {
ans.add(arr[i]);
map.put(arr[i], 1);
}
}
return ans;
}


public static void main(String[] args) {
int[] arr = {1,3,3,5,5,8,7,9,10};
System.out.print(sol(arr));

}


Post a Comment

0 Comments