-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
a-needle-in-the-haystack.js
175 lines (146 loc) · 4.35 KB
/
a-needle-in-the-haystack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
function findNeedle(haystack) {
const index = haystack.indexOf('needle');
return `found the needle at position ${index}`;
}
function findNeedle(haystack) {
// a place to store the index
let index;
// iterate over the haystack
for (let i = 0; i < haystack.length; i++) {
const currentValue = haystack[i];
// if the current value is equal to 'needle'
if (currentValue === 'needle') {
// store the current index in the index variable
index = i;
// break out of the for loop
break;
}
}
return `found the needle at position ${index}`;
}
function indexOf(array, value) {
let index = -1;
for (let i = 0; i < array.length; i++) {
const currentValue = array[i];
if (currentValue === value) {
index = i;
break;
}
}
return index;
}
function findNeedle(haystack) {
const index = indexOf(haystack, 'needle');
return `found the needle at position ${index}`;
}
function findNeedle(haystack) {
for (let i = 0; i < haystack.length; i++) {
const currentValue = haystack[i];
if (currentValue === 'needle') {
return `found the needle at position ${i}`;
}
}
}
function findNeedle(haystack) {
const index = haystack.findIndex(value => value === 'needle');
return `found the needle at position ${index}`;
}
function findNeedle(haystack) {
let index;
let leftPointer = 0;
let rightPointer = haystack.length - 1;
while (leftPointer <= rightPointer) {
const leftValue = haystack[leftPointer];
if (leftValue === 'needle') {
index = leftPointer;
break;
}
const rightValue = haystack[rightPointer];
if (rightValue === 'needle') {
index = rightPointer;
break;
}
leftPointer++;
rightPointer--;
}
return `found the needle at position ${index}`;
}
function findNeedle(haystack) {
let index;
let leftPointer = 0;
let rightPointer = haystack.length - 1;
while (leftPointer <= rightPointer) {
const leftValue = haystack[leftPointer++];
if (leftValue === 'needle') {
index = leftPointer - 1;
break;
}
const rightValue = haystack[rightPointer--];
if (rightValue === 'needle') {
index = rightPointer + 1;
break;
}
}
return `found the needle at position ${index}`;
}
function findNeedle(haystack) {
let index;
let leftPointer = 0;
let rightPointer = haystack.length - 1;
while (leftPointer <= rightPointer) {
const leftValue = haystack[++leftPointer];
if (leftValue === 'needle') {
index = leftPointer;
break;
}
const rightValue = haystack[--rightPointer];
if (rightValue === 'needle') {
index = rightPointer;
break;
}
}
return `found the needle at position ${index}`;
}
// J B
function findNeedle(arr, i = 0) {
if(arr[i] === 'needle') return i;
return findNeedle(arr, ++i);
}
function findNeedle(haystack) {
let index;
haystack.forEach((currentValue, i) => {
if (currentValue === 'needle') {
index = i;
}
});
return `found the needle at position ${index}`;
}
var haystack_0 = ['needle', '3', '123124234', undefined, 'world', 'hay', 2, '3', true, false];
var haystack_1 = ['3', '123124234', undefined, 'needle', 'world', 'hay', 2, '3', true, false];
var haystack_2 = ['283497238987234', 'a dog', 'a cat', 'some random junk', 'a piece of hay', 'needle', 'something somebody lost a while ago'];
var haystack_3 = [1,2,3,4,5,6,7,8,8,7,5,4,3,4,5,6,67,5,5,3,3,4,2,34,234,23,4,234,324,324,'needle',1,2,3,4,5,5,6,5,4,32,3,45,54];
console.log(
findNeedle(haystack_0),
'found the needle at position 0'
);
console.log(
findNeedle(haystack_1),
'found the needle at position 3'
);
console.log(
findNeedle(haystack_2),
'found the needle at position 5'
);
console.log(
findNeedle(haystack_3),
'found the needle at position 30'
);
// Pranjal
// Use two pointer approach to solve this problem (i.e. assign two pointer one for left side and one for right side and move them + check for needle)
// Also you can try recursion to solve this
// J B
// function recursiveNeedle(arr,i=0) { if(arr[i]==="needle")return i; return recursiveNeedle(arr,i++); }
// PRANJAL AGNIHOTRI
// Hii CJ can you please help me with this problem, just a idea will help. You can google leetcode "maximum-length-of-a-concatenated-string-with-unique-characters". Please
// Spacie
// could you make a haystack pun and implement it using only while and pop?