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

51,876 answers

573 users

How to find the first repeating element in an array of integers with C

1 Answer

0 votes
#include <stdio.h>

int element_exist(int arr[], int len, int start, int n) { 
    for (int i = start; i <= len; i++) { 
        if (arr[i] == n)
            return 1;
    }
    return 0;
}   

int get_first_repeating_element(int arr[], int len) { 
    
    for (int i = 0; i <= len; i++) { 
        if (element_exist(arr, len, i + 1, arr[i])) 
            return arr[i];
    }
    return -1;
} 
  
int main() 
{ 
    int arr[] = {1, 2, 4, 5, 6, 5, 4, 3, 7}; 
  
    int n = get_first_repeating_element(arr, sizeof(arr) / sizeof(arr[0])); 
    
    if (n != -1) 
        printf("First repeating element is: %i", n); 
    else
        printf("No repeating elements"); 
    
}  




/*
run:

First repeating element is: 4

*/

 



answered May 13, 2019 by avibootz
...