Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Exercise
- {
- public static void main(String [] args)
- {
- Scanner console= new Scanner(System.in);
- System.out.println("Input the E value you wish to use");
- int e = console.nextInt();
- LinkedList<Integer> list = new LinkedList<>();
- partition(list, e);
- countUnique(list);
- }
- //ex 4
- public static void partition(List<Integer>list, int value) {
- List<Integer> temp = new LinkedList<Integer>();
- Iterator<Integer> itr = list.iterator();
- while (itr.hasNext()) {
- int element = itr.next();
- if (element < value) {
- temp.add(0, element);
- } else {
- temp.add(element);
- }
- }
- }
- //ex 6
- public static int countUnique(LinkedList<Integer> list)
- {
- Set<Integer> set = new HashSet<Integer>();
- Iterator<Integer> itr = list.iterator();
- while (itr.hasNext()) {
- int temp = itr.next();
- if (set.contains(temp)) {
- break;
- } else
- {
- set.add(temp);
- }
- }
- return set.size();
- }
- //ex 8
- public static String maxLength(String[] array) {
- int maxLength = 0;
- String longestString = null;
- if(array.length == 0)
- {
- return null;
- }
- for (String s : array) {
- if (s.length() > maxLength) {
- maxLength = s.length();
- longestString = s;
- }
- }
- return longestString;
- }
- //ex 10
- public static void removeEvenLengths(String[] array)
- {
- for (String s : array) {
- if (s.length() % 2 == 0)
- {
- int x = array.get(s);
- array.remove(x);
- }
- }
- }
- public static boolean contains3(String[] array)
- {
- Map<String, Integer> WordMap = new TreeMap<>();
- for(String word : array)
- {
- if(WordMap.containsKey(word))
- {
- int count = WordMap.get(word);
- WordMap.put(word, count + 1);
- }
- else
- {
- WordMap.put(word, 1);
- }
- }
- for(String word: WordMap.keySet())
- {
- int count = WordMap.get(word);
- if(count == 3)
- {
- return true;
- }
- }
- return false;
- }
- //ex 14
- public static Map<String, Integer> intersect(TreeMap<String, Integer> map1, TreeMap<String, Integer> map2)
- {
- Map<String, Integer>FinalMap = new TreeMap<>();
- for(String s1 : map1.keySet())
- {
- for(String s2 : map2.keySet())
- {
- if(s1.equals(s2))
- {
- if(map1.get(s1) == map2.get(s2))
- {
- FinalMap.put(s1, map1.get(s1));
- }
- }
- }
- }
- return FinalMap;
- }
- //ex 16
- public static boolean is1total(TreeMap<String, Integer> map1)
- {
- Map<String, Integer> temp = map1;
- for(String s1 : temp.keySet())
- {
- for(String s2 : map1.keySet())
- {
- if(!s1.equals(s2) && temp.get(s1) == map1.get(s2))
- {
- return false;
- }
- }
- }
- return true;
- }
- //ex 18
- public static Map<String, String> reverse(TreeMap<String, String> map1)
- {
- Map<String, String> Final = new TreeMap<>();
- for(String s : map1.keySet())
- {
- String s1 = map1.get(s);
- Final.put(s1, s);
- }
- return Final;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment