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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 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 145 views
1 answer 123 views
1 answer 189 views
1 answer 106 views
1 answer 164 views
...