✅ 3ms fasterthan100 || greedy || TC: O(n), SC: O(1) || 2 ways
If you\'ll like, do UpVote :)\n\nNote for Coding Interviews:\n\t\n\t1.  I mentioned 2 solutions keeping Coding interviews in mind, where you can first talk abou
If you\'ll like, do **UpVote** :)\n\n**Note for Coding Interviews:**\n\t\n\t1.  I mentioned 2 solutions keeping Coding interviews in mind, where you can first talk about Solution 1 and,\n\t\t then optimize it right after using Solution 2 :) \n\t2.  It is always recommended to start with brute force and then bring more optimized approach in Coding interviews.\n\t3.  Solution 2 is almost similar to first, just without creating additional List and without deletion of elements.\n\t\n\t\n## Solution 1 (less optimized, with deletions)\n\tclass Solution {\n\t\tpublic int minDeletion(int[] nums) {\n\t\t\tList<Integer> list = new ArrayList<>();\n\t\t\tfor(int num : nums) { list.add(num); }\n\t\t\tif(allSameElements(list)){ return nums.length; }\n\n\t\t\tint steps = 0;    \n\t\t\tsteps = makeEvenIndexUnique(list, steps);\n\t\t\tsteps = makeEvenLength(list, steps);\n\t\t\treturn steps;\n\t\t}\n\n\t\tpublic int makeEvenIndexUnique(List<Integer> list, int steps){\n\t\t\tfor(int i = 0; i < list.size()-1; i+=2){\n\t\t\t\tif (i%2 != 0) continue;\n\t\t\t\tif(list.get(i).equals(list.get(i+1))){  \n\t\t\t\t\tlist.remove(i);\n\t\t\t\t\tsteps++; \n\t\t\t\t\ti-=2;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn steps;\n\t\t}\n\t\tpublic int makeEvenLength(List<Integer> list, int steps){\n\t\t\tif(list.size() % 2 != 0){ steps++; }\n\t\t\treturn steps;\n\t\t}\n\n\t\tpublic boolean allSameElements(List<Integer> list){\n\t\t\tSet<Integer> set = new HashSet<>();\n\t\t\t\tset.addAll(list);\n\t\t\t\tif(set.size() == 1){ return true; }\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t//\n\t//\n\t//\n\n## \tSolution 2 (more optimized, no deletions)\n\tclass Solution {\n\t\tpublic int minDeletion(int[] nums) {\n\t\t\tint steps = 0;    \n\t\t\tsteps = makeEvenIndexUnique(nums, steps);\n\t\t\tsteps = makeEvenLength(nums, steps);\n\t\t\treturn steps;\n\t\t}\n\n\t\tpublic int makeEvenIndexUnique(int[] nums, int steps){\n\t\t\tfor(int i = 0; i < nums.length-1; i+=2){\n\t\t\t\tif(nums[i] == nums[i+1]){  \n\t\t\t\t\tsteps++; \n\t\t\t\t\ti--;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn steps;\n\t\t}\n\t\tpublic int makeEvenLength(int[] nums, int steps){\n\t\t\tif( (nums.length - steps) % 2 != 0){ steps++; }\n\t\t\treturn steps;\n\t\t}\n\t}\n\t\nFor coding interviews:\nYou can discuss Solution 1 first, and then optimize using Solution 2 (instead of directly jumpting to optimized solution). \n\nIf you need more explanation or, if got even more optimized way, do share.\nIf you liked it, do **UpVote** :)\n\n
['Java']