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,907 questions

51,839 answers

573 users

How to remove duplicate elements from int array in C

1 Answer

0 votes
#include <stdio.h>

#define LEN 8

int main() {
    int arr[LEN] = {2, 3, 2, 6, 3, 7, 9, 9};
    int len = LEN;
    
    for (int i = 0; i < len; i++) {
        for (int j = i + 1; j < len;) {
            if (arr[j] == arr[i]) {
                for (int k = j; k < len; k++) {
                    arr[k] = arr[k + 1];
                }
                len--;
            }
            else {
                j++;
            }
        }
    }

    for (int i = 0; i < len; i++) {
        printf("%2d", arr[i]);
    }

    return 0;
}



/*
run:

 2 3 6 7 9

*/

 



answered Jun 30, 2019 by avibootz
edited Sep 27, 2020 by avibootz

Related questions

1 answer 234 views
1 answer 147 views
1 answer 163 views
1 answer 150 views
1 answer 162 views
...