Question Detail

I have an array Of

String[] arr={"Google","Google","Facebook","Gmail","Twitter","Twitter"};

how to can I remove all Duplicate Elements values? this Array should be converted into 

String[] arr={"Google","Facebook","Gmail","Twitter"};


Thread Reply

Nick Johnson

- 11 months ago

you can take the help of Set collection

int[] arr={1,1,1,3,4,3,5,6,6};
int endsVal = arr.length;
Set<Integer> set = new HashSet<Integer>();

for(int i = 0; i < endsVal ; i++){
  set.add(arr[i]);
}

now if you will iterate through this set, it will contain only unique values. Iterating code is like this :

Iterator itr = set.iterator();
while(itr.hasNext()) {
  System.out.println(itr.next());
}

alex levine

- 11 months ago

int[] nums = {5,2,7,2,4,7,8,2,3};    

        for(int i=0;i<nums.length;i++){

            boolean isDistinct = false;

            for(int j=0;j<i;j++){

                if(nums[i] == nums[j]){

                    isDistinct = true;

                    break;

                }

            }

            if(!isDistinct){

                System.out.print(nums[i]+" ");

            }

        }

    }

Nick Johnson

- 11 months ago

You can also use this

String[] arr={"Google","Google","Facebook","Gmail","Twitter","Twitter"};
List<String> newArr= ???????new ArrayList<String>();
for(int i=0; i<arr.length; i++){
if(Arrays.asList(newArr???????).contains(arr[i]???????)){
newArr.add(arr[i]);
}
}