Find First Non-Repeating Element In An Array | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Nice approach and simple to follow, we can actually delete repeating elements and solve it this way, I have no idea which approach is faster :

int flag = 0;
int array[] = {1, 1, 1, 2, 2, 2, 2, 2, 24, 55};
int element ;
int length = sizeof array / sizeof array[0] ;
for(int i = 0; i < length ; i++){
element = array[i];
flag = 0;
for(int j = i+1 ; j < length ; j++){
if(element == array[j]){
// we delete the repeating element by shifting the array to the left
for(int k = j ; k < length - 1 ; k++){
array[k] = array[k+1];
}
--length;
--j;
flag = 1;
}
}
// if flag is 0 we break because we have found the first Non-repeating element
if(flag == 0 ){
break;
}
}
if(flag == 1){
printf("there is no Non-repeating element!!\n");
}
else {
printf("%d is the first Non-repeating element\n", element);
}

justcurious
Автор

We saw this algorithm at university. Eventually you can delete repeating numbers by pausing the inner FOR cycle and creating a new FOR cycle that shift all numbers and decrease the array size

Sufian
Автор

meh, O(n^2) time complexity. There’s a pretty easy O(n) solution.

itsmaxim
Автор

Nah, this is not a quality program we have seen before. The i index does not need to go the full length. The j index can start at i+1. The second boolean variable is just a kind of copy of the first. At least the subject is interesting.

FritsvanDoorn