I having a problem with coding this:
Write a static method named removeDuplicates
that takes as input an array of integers and returns as a result a new
You could also put the array elements into a Set
for which the semantics precisely are that it contains no duplicate elements.
try this -
public static int[] removeDuplicates(int []s){
int result[] = new int[s.length], j=0;
for (int i : s) {
if(!isExists(result, i))
result[j++] = i;
}
return result;
}
private static boolean isExists(int[] array, int value){
for (int i : array) {
if(i==value)
return true;
}
return false;
}
This is an interview question. Question : Remove Duplicates from an array in place :
public class Solution4 {
public static void main(String[] args) {
int[] a = {1,1,2,3,4,5,6,6,7,8};
int countwithoutDuplicates = lengthofarraywithoutDuplicates(a);
for(int i = 0 ; i < countwithoutDuplicates ; i++) {
System.out.println(a[i] + " ");
}
}
private static int lengthofarraywithoutDuplicates(int[] a) {
int countwithoutDuplicates = 1 ;
for (int i = 1; i < a.length; i++) {
if( a[i] != a[i-1] ) {
a[countwithoutDuplicates++] = a[i];
}//if
}//for
System.out.println("length of array withpout duplicates = >" + countwithoutDuplicates);
return countwithoutDuplicates;
}//lengthofarraywithoutDuplicates
}
In Python :
def lengthwithoutduplicates(nums):
if not nums: return 0
if len(nums) == 1:return 1
# moving backwards from last element i.e.len(a) -1 to first element 0 and step is -1
for i in range(len(nums)-1,0,-1):
# delete the repeated element
if nums[i] == nums[i-1]: del nums[i]
# store the new length of the array without the duplicates in a variable
# and return the variable
l = len(a)
return l
a = [1, 1, 2, 3, 4, 5, 6, 6, 7, 8];
l = lengthwithoutduplicates(a)
for i in range(1,l):print(i)
In Python: list comprehension using enumerate
a = [1, 1, 2, 3, 4, 5, 6, 6, 7, 8]
aa = [ ch for i, ch in enumerate(a) if ch not in a[:i] ]
print(aa) # output => [1, 2, 3, 4, 5, 6, 7, 8]
This worked for me:
import java.util.Arrays;
import java.util.HashSet;
public class Util {
public static int[] removeDups(final int[] intArrayWithDups) {
final int[] intArrayDupsRemoved = new int[intArrayWithDups.length];
final HashSet<Integer> alreadyAdded = new HashSet<>();
int innerCounter = 0;
for (int integer : intArrayWithDups) {
if (!alreadyAdded.contains(integer)) {
intArrayDupsRemoved[innerCounter] = integer;
alreadyAdded.add(intArrayDupsRemoved[innerCounter]);
innerCounter++;
}
}
return Arrays.copyOf(intArrayDupsRemoved, innerCounter);
}
}
What you have to do is , you have to check for each element in second array whether previous element is already present or not.
You can use better approach Use HashSet and return set.
public static Set removeDuplicates(int []s){
Set<Integer> set = new HashSet<Integer>();
for(int i=0;i<s.length;++i){
set.add(s[i]);
}//endFori
return set;
}//endMethod
If you need int Array than take a look of this java-hashsetinteger-to-int-array link.
public class DistinctNumbers{
public static void main(String[] args){
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter ten numbers: ");
int[] numbers = new int[10];
for(int i = 0; i < numbers.length; ++i){
numbers[i] = input.nextInt();
}
System.out.println("The distinct numbers are:");
System.out.println(java.util.Arrays.toString(eliminateDuplicates(numbers)));
}
public static int[] eliminateDuplicates(int[] list){
int[] distinctList = new int[list.length];
boolean isDuplicate = false;
int count = list.length-1;
for(int i = list.length-1; i >= 0; --i){
isDuplicate = false;
for(int j = i-1; j >= 0 && !isDuplicate; --j){
if(list[j] == list[i]){
isDuplicate = true;
}
}
if(!isDuplicate){
distinctList[count--] = list[i];
}
}
int[] out = new int[list.length-count-1];
System.arraycopy(distinctList, count+1, out, 0, list.length-count-1);
return out;
}
}