pkshah912
BAN USERpublic static int findNonRepeatedElement(int[] array){
int firstNum = -1;
Map<Integer, Boolean> map = new LinkedHashMap<>();
for(int index = 0;index<array.length;index++){
if(!map.containsKey(array[index])){
map.put(array[index], false);
}
else
map.put(array[index], true);
}
for(Entry<Integer, Boolean> e: map.entrySet()){
if(!e.getValue()){
firstNum = e.getKey();
break;
}
}
return firstNum;
}
public class MinMax {
public static void main(String args[]){
int[] array = {1,2,3,4,5,6,7};
Arrays.sort(array);
int minMaxArray[] = new int[array.length];
for(int index =0,firstPos =0, lastPos = array.length-1;index<array.length;index++){
if(index % 2 != 0){
minMaxArray[index] = array[firstPos];
firstPos++;
}
else{
minMaxArray[index] = array[lastPos];
lastPos--;
}
}
System.out.println("Input:" + Arrays.toString(array));
System.out.println("Output:" + Arrays.toString(minMaxArray));
}
}
public class FirstEvenOccurences {
public static void main(String args[]){
int[] arr = {1,4,3,5,6,2,4,2};
HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
for(int element: arr){
if(map.get(element) == null)
map.put(element, false);
else{
if(map.get(element) == true)
map.put(element, false);
else
map.put(element, true);
}
}
for(int element: arr){
if(map.get(element) == true){
System.out.println("First even occurence: " + element);
break;
}
}
}
}
- pkshah912 January 06, 2016