Mark As Completed Discussion

Brute Force Approach:

To identify and eliminate duplicates simultaneously, we may generate a new array that is the exact length of the original array and compare each element to its neighbor. In the case of a successful comparison, we would transfer one instance of the matching value from the original to the new array.

Solution Steps:

  • Return the array by checking if the array's length is 0 or 1.
  • Declare a variable temp to store the single instance of each element.
  • The input array will be scanned and copied a single instance of each element from arr to temp.
  • The variable p will be tracking the count of the single instance of each element.
  • We will copy each element's single instance from temp to arr.
  • And last, we will return the length of the array and the resultant arr by eliminating the duplicated element.

Brute Force Approach

Here's to reference code for a better understanding!

1var ob1;
2
3class BruteApproach{
4    removeDuplicates(arr,N) {
5        if (N === 0 || N === 1) {
6            return N;
7        }
8
9        var temp = new Array(N);		
10		var p = 0;
11        for (let i=0; i<N-1; i++){
12            if (arr[i] != arr[i+1]){
13                temp[p++] = arr[i];
14            }				
15        }
16        temp[p++] = arr[N-1];
17        for (let i=0; i<p; i++){
18            arr[i] = temp[i];
19        }
20		return p;
21    }
22}
23
24
25ob1 = new BruteApproach();
26nums=[2,3,4,4,5,6,6,6,7,8,8]
27a=ob1.removeDuplicates(nums, nums.length);
28var newarr = new Array(a);
29for (let z=0;z<a;z++){
30    newarr[z]=nums[z];
31}
32console.log(newarr);