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!! "); } else { printf("%d is the first Non-repeating element ",element); }
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
I remember trying to implement strtok() function by myself without looking to any tutorial, I ended up doing it in 1 function with 4 nested loops, I spent few days trying to do it that way, and after another few days I couldn't do it again , I don't know if I have some kind of programming syndrome or is it some kind of retardness, today I did it but a little bit faster , just few hours : 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!! "); } else { printf("%d is the first Non-repeating element ",element); } I'm not sure if I can write the same code again hhhh.
@@justcurious1940 Beautiful. Well done. Today i implemented an algorithm that sorts an array of elements while reading with scanf by keyboard or fscanf by file. Sorry for my dirty english. I applied this algorithm to read strings from a text file and sort them alphabetically in arrays of struct. Please check my linear system solver demo video on my channel and share it.
The goal for videos like this covering introductory problems is learning about algorithms and how to implement them, and the target audience is beginners. There's typically a number of ways to solve these problems, and in this case yes we can make an O(n) algorithm but the one I'm aware of will come at the cost of more space/memory. If I cover everything in one video they would become too long for most people to watch. Maybe one day I can do the O(n) version solution though too as its own video!
@marc-andreburn8942 The problem is you could have an array like this: int array[] = {1,2,3,4,5,1,7,3,4,5,2} where 7 is the first non-repeating character. But with that as the inner loop, when i reaches index 5 where the 2nd 1 value is, the aglortihm will identify THIS as the first non-repeating character, because if all we do is check from this position until the the end of the array we'll never find another 1 value (because the repeating 1 value comes before the i position).
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.
No, we cannot start the index j at i+1. For example if we have this array: int array[] = {1,2,3,4,5,1,7,2,3,4,5} The first non-repeating element is 7 which is at index 6. But if we start j off at i +1, then the '1' at index 5 will be identified as the first non-repeating element. Because we will check for '1' from the index 6 onwards and it will not be found (but there is another '1' at index 0). And no, the second boolean is not a copy of the first. The first boolean is helping us to identify if the current element being examined is a repeat or not. The second boolean is letting us know if ANY non-repeating element was ever found.
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!!
");
}
else {
printf("%d is the first Non-repeating element
",element);
}
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
I remember trying to implement strtok() function by myself without looking to any tutorial, I ended up doing it in 1 function with 4 nested loops, I spent few days trying to do it that way, and after another few days I couldn't do it again , I don't know if I have some kind of programming syndrome or is it some kind of retardness, today I did it but a little bit faster , just few hours :
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!!
");
}
else {
printf("%d is the first Non-repeating element
",element);
}
I'm not sure if I can write the same code again hhhh.
@@justcurious1940 Beautiful. Well done. Today i implemented an algorithm that sorts an array of elements while reading with scanf by keyboard or fscanf by file. Sorry for my dirty english. I applied this algorithm to read strings from a text file and sort them alphabetically in arrays of struct. Please check my linear system solver demo video on my channel and share it.
very well explained!!!
I'm glad you liked it! :-)
meh, O(n^2) time complexity. There’s a pretty easy O(n) solution.
The goal for videos like this covering introductory problems is learning about algorithms and how to implement them, and the target audience is beginners. There's typically a number of ways to solve these problems, and in this case yes we can make an O(n) algorithm but the one I'm aware of will come at the cost of more space/memory. If I cover everything in one video they would become too long for most people to watch. Maybe one day I can do the O(n) version solution though too as its own video!
i wondrer why, in the inner loop j begin with 0?
it will be faster with : (int j=i+1, j
@marc-andreburn8942 The problem is you could have an array like this:
int array[] = {1,2,3,4,5,1,7,3,4,5,2}
where 7 is the first non-repeating character. But with that as the inner loop, when i reaches index 5 where the 2nd 1 value is, the aglortihm will identify THIS as the first non-repeating character, because if all we do is check from this position until the the end of the array we'll never find another 1 value (because the repeating 1 value comes before the i position).
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.
No, we cannot start the index j at i+1. For example if we have this array:
int array[] = {1,2,3,4,5,1,7,2,3,4,5}
The first non-repeating element is 7 which is at index 6. But if we start j off at i +1, then the '1' at index 5 will be identified as the first non-repeating element. Because we will check for '1' from the index 6 onwards and it will not be found (but there is another '1' at index 0).
And no, the second boolean is not a copy of the first. The first boolean is helping us to identify if the current element being examined is a repeat or not. The second boolean is letting us know if ANY non-repeating element was ever found.
@@PortfolioCourses uhm, yes, you are right. I missed that. Thank you. I learned something.