Quick actions

cmd+k|ctrl+k

Navigation

Languages

Find all indices of a given element in sorted form of given Array

Snippet info

Language

JavaScript

Visibility

public

Author

soham57

Created

2023-01-22T04:31:05.329215Z

Updated

2023-01-22T04:31:05.329215Z

function indexesOfElement(array, target){
    let smCount=0;
    let occurence=0;
    for(let x of array){
        if(x<target){
            smCount++;
        }
        
        else if(x===target){
            occurence++;
        }
    }
    let result=[]
    if(occurence==0){
        return []
    }
    
    else{
        while(occurence--){
            result.push(smCount);
            smCount++;
        }
    }
    
    return result;
}

console.log(indexesOfElement([1, 2, 5, 2, 3],3))
INFO