Remove Duplicate Item from array using HashMap in Java
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));
}
}
Other Blog you might be interested
0 Comments
if you are not getting it then ask i am glad to help