Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,039 questions

40,767 answers

573 users

How to remove duplicates from integer array without collection in Java

2 Answers

0 votes
import java.util.BitSet;

public class MyClass {
    public static void main(String args[]) {
        int[] arr = { 2, 4, 1, 1, 3, 5, 1, 2, 3, 3, 3, 7 };
        BitSet bs = new BitSet(arr.length);
        for (int i = 0; i < arr.length; i++) {
            if (bs.get(arr[i])) {
                arr[i] = 0;
            } else {
                bs.set(arr[i]);
                }
        }
 
        for (int n : arr) {
            System.out.print(n + ", ");
        }
    }
}


/*
run:

2, 4, 1, 0, 3, 5, 0, 0, 0, 0, 0, 7, 

*/

 





answered Jul 25, 2020 by avibootz
edited Jul 25, 2020 by avibootz
0 votes
import java.util.ArrayList;

public class MyClass {
    public static void main(String args[]) {
        int[] arr = { 2, 4, 1, 1, 3, 5, 1, 2, 3, 3, 3, 7 };
        ArrayList al = new ArrayList();

        for (int i = 0; i < arr.length; i++){
            if(!al.contains(arr[i])){
                al.add(arr[i]);
            }
            else {
                arr[i] = 0;
            }
        }
        for (int n : arr) {
            System.out.print(n + ", ");
        }
    }
}


/*
run:

2, 4, 1, 0, 3, 5, 0, 0, 0, 0, 0, 7, 

*/

 





answered Jul 25, 2020 by avibootz
edited Jul 25, 2020 by avibootz

Related questions

1 answer 39 views
1 answer 57 views
1 answer 84 views
1 answer 128 views
1 answer 79 views
...