Input: int[] arr = {1,1,2,2,3,4,4,5,5,5,1,1,3,6};
Output: 6
Answer:
Java version:
import java.util.HashMap;
public class Uniquenum {
public static int uniqueMy(int[] a){
HashMap<Integer,Boolean> hmap = new HashMap<Integer,Boolean>();
for(int i=0;i<a.length;++i){
if(hmap.containsKey(a[i])== false){
hmap.put(a[i],true);
}
else if(hmap.get(a[i])==true){
hmap.put(a[i],false);
}
}
for(int i=0;i<a.length;++i){
if(hmap.get(a[i]) == true){
return a[i];
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1,1,2,2,3,4,4,5,5,5,1,1,3,6};
int uniquenum = uniqueMy(arr);
System.out.println("Unique number is: " + uniquenum);
}
}
No comments:
Post a Comment